[R] Newbie: how to get unique x unique x aggregate chart?
I'm very new to R, so please forgive me if I just missed the answer in existing documentation... I have a data set with at least three columns, X, Y, and Z. I want to produce a chart where one axis shows all the unique values of X, and the other axis shows all the unique values of Y. Each cell within the chart should contain the result of applying an aggregate function (such as mean(), for example) to Z value of those rows that are associated with that cell (via their X and Y values). Can someone recommend a good way to do this? Thanks very much, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
Christian Convey wrote: I'm very new to R, so please forgive me if I just missed the answer in existing documentation... I have a data set with at least three columns, X, Y, and Z. I want to produce a chart where one axis shows all the unique values of X, and the other axis shows all the unique values of Y. Each cell within the chart should contain the result of applying an aggregate function (such as mean(), for example) to Z value of those rows that are associated with that cell (via their X and Y values). Can someone recommend a good way to do this? I'm not sure exactly what kind of chart you want, but this may give you some ideas. library(reshape) # Provides melt() and cast() df - data.frame(X = rep(c(A,B,C,D), each = 40), Y = rep(1:4, 40), Z = runif(160)) df - melt(df, measure.var=Z) newdf - cast(df, X + Y ~ ., fun.aggregate = mean) library(lattice) # Provides barchart and bwplot() barchart(value ~ X | Y, data = newdf, layout=c(4,1,1)) barchart(value ~ X, groups=Y, data = newdf, auto.key=TRUE) Also, you might consider this which shows much more than the location of Z: bwplot(Z ~ X | Y, data = df, layout=c(4,1,1)) Finally, here is another possibly useful way to aggregrate df (an alternative to melt() and cast() which gives a different structure): with(df, tapply(Z, list(X,Y), mean)) Thanks very much, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
I'm not sure I understand the question, but you might look into the following functions: unique heatmap image Again, if I understand the question, you would create a length(unique (x)) by length(unique(y)) sized matrix, and fill it with appropriate values of z. Then pass that to heatmap or image. Hope that helps--feel free to tell me if I've answered the wrong question, Jeff. On Nov 15, 2006, at 8:30 AM, Christian Convey wrote: I'm very new to R, so please forgive me if I just missed the answer in existing documentation... I have a data set with at least three columns, X, Y, and Z. I want to produce a chart where one axis shows all the unique values of X, and the other axis shows all the unique values of Y. Each cell within the chart should contain the result of applying an aggregate function (such as mean(), for example) to Z value of those rows that are associated with that cell (via their X and Y values). Can someone recommend a good way to do this? Thanks very much, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
Thanks, let me try to clarify my question with an example. Suppose I have the following data: Gender, Major, Course-Grade F, Psy, 3.5 F, Psy, 3.1 M, Hst, 3.7 F, Hst, 3.6 M, Hst, 2.6 M, Eng, 3.9 I want to compute a table like the following: X-axis: Gender Y-axis: Major Cell(x,y) = mean course-grade So for example, with the data above: F M Psy | 3.3NA Hst | 3.63.15 Eng | NA3.9 If I were doing this in SQL I'd do it with a cross-tab query. But the world of R still has much unfamiliar terrain :) Thanks, Christian On 11/15/06, Jeffrey Robert Spies [EMAIL PROTECTED] wrote: I'm not sure I understand the question, but you might look into the following functions: unique heatmap image Again, if I understand the question, you would create a length(unique (x)) by length(unique(y)) sized matrix, and fill it with appropriate values of z. Then pass that to heatmap or image. Hope that helps--feel free to tell me if I've answered the wrong question, Jeff. On Nov 15, 2006, at 8:30 AM, Christian Convey wrote: I'm very new to R, so please forgive me if I just missed the answer in existing documentation... I have a data set with at least three columns, X, Y, and Z. I want to produce a chart where one axis shows all the unique values of X, and the other axis shows all the unique values of Y. Each cell within the chart should contain the result of applying an aggregate function (such as mean(), for example) to Z value of those rows that are associated with that cell (via their X and Y values). Can someone recommend a good way to do this? Thanks very much, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
On Wed, 2006-11-15 at 15:03 -0500, Christian Convey wrote: Thanks, let me try to clarify my question with an example. Suppose I have the following data: Gender, Major, Course-Grade F, Psy, 3.5 F, Psy, 3.1 M, Hst, 3.7 F, Hst, 3.6 M, Hst, 2.6 M, Eng, 3.9 I want to compute a table like the following: X-axis: Gender Y-axis: Major Cell(x,y) = mean course-grade So for example, with the data above: F M Psy | 3.3NA Hst | 3.63.15 Eng | NA3.9 If I were doing this in SQL I'd do it with a cross-tab query. But the world of R still has much unfamiliar terrain :) Thanks, Christian Presuming that DF is a data frame containing your data: with(DF, tapply(Course.Grade, list(Major, Gender), mean, na.rm = TRUE)) FM Eng NA 3.90 Hst 3.6 3.15 Psy 3.3 NA See ?tapply and ?with HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
Marc's solution looks a bit easier but here are a few more anyways: # 1 reshape(DF, dir = wide, timevar = Gender, idvar = Major) # 2 library(reshape) DFm - melt(DF, id = 1:2) cast(DFm, Major ~ Gender, fun = mean) On 11/15/06, Christian Convey [EMAIL PROTECTED] wrote: Thanks, let me try to clarify my question with an example. Suppose I have the following data: Gender, Major, Course-Grade F, Psy, 3.5 F, Psy, 3.1 M, Hst, 3.7 F, Hst, 3.6 M, Hst, 2.6 M, Eng, 3.9 I want to compute a table like the following: X-axis: Gender Y-axis: Major Cell(x,y) = mean course-grade So for example, with the data above: F M Psy | 3.3NA Hst | 3.63.15 Eng | NA3.9 If I were doing this in SQL I'd do it with a cross-tab query. But the world of R still has much unfamiliar terrain :) Thanks, Christian On 11/15/06, Jeffrey Robert Spies [EMAIL PROTECTED] wrote: I'm not sure I understand the question, but you might look into the following functions: unique heatmap image Again, if I understand the question, you would create a length(unique (x)) by length(unique(y)) sized matrix, and fill it with appropriate values of z. Then pass that to heatmap or image. Hope that helps--feel free to tell me if I've answered the wrong question, Jeff. On Nov 15, 2006, at 8:30 AM, Christian Convey wrote: I'm very new to R, so please forgive me if I just missed the answer in existing documentation... I have a data set with at least three columns, X, Y, and Z. I want to produce a chart where one axis shows all the unique values of X, and the other axis shows all the unique values of Y. Each cell within the chart should contain the result of applying an aggregate function (such as mean(), for example) to Z value of those rows that are associated with that cell (via their X and Y values). Can someone recommend a good way to do this? Thanks very much, Christian [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Newbie: how to get unique x unique x aggregate chart?
That did it! Thanks Marc. - Christian On 11/15/06, Marc Schwartz [EMAIL PROTECTED] wrote: On Wed, 2006-11-15 at 15:03 -0500, Christian Convey wrote: Thanks, let me try to clarify my question with an example. Suppose I have the following data: Gender, Major, Course-Grade F, Psy, 3.5 F, Psy, 3.1 M, Hst, 3.7 F, Hst, 3.6 M, Hst, 2.6 M, Eng, 3.9 I want to compute a table like the following: X-axis: Gender Y-axis: Major Cell(x,y) = mean course-grade So for example, with the data above: F M Psy | 3.3NA Hst | 3.63.15 Eng | NA3.9 If I were doing this in SQL I'd do it with a cross-tab query. But the world of R still has much unfamiliar terrain :) Thanks, Christian Presuming that DF is a data frame containing your data: with(DF, tapply(Course.Grade, list(Major, Gender), mean, na.rm = TRUE)) FM Eng NA 3.90 Hst 3.6 3.15 Psy 3.3 NA See ?tapply and ?with HTH, Marc Schwartz [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.