afpjt@uaa.alaska.edu
2005-Jan-03 23:38 UTC
[R] Two brief questions concerning sapply. Can anyone please help?!
To anyone who can help: I have two brief questions concerning sapply. Following below is the code for my example. The two problems are described at the end of the code: site <- rep(2:6, each = 12) tillage <- rep(c(1,-1), each = 6, times = 5) carbon <- c(18.23, 16.06, 17.81, 16.07, 17.26, 17.08, 14.92, 15.88, 12.11, 14.23, 16.99, 13.57, 20.34, 20.3, 18.78, 19.91, 18.43, 20.8, 18.98, 19.24, 18.18, 16.69, 19.72, 18.35, 36.83, 32.85, 30.21, 31.58, 34.35, 31.4, 27.72, 29.9, 25.97, 27.63, 28.13, 27.68, 23.11, 24.32, 21.87, 29.18, 26.52, 23.25, 24.16, 17.62, 19.62, 20.5, 21.01, 25.62, 25.73, 23.42, 25.17, 23.76, 23.27, 23.37, 20.89, 19.93, 19.24, 19.4, 22.09, 18.96) junk <- interaction(tillage, site) nr.carbon <- sapply(split(carbon, junk), function(x) mean(x))> nr.carbon-1.2 1.2 -1.3 1.3 -1.4 1.4 -1.5 1.5 14.61667 17.08500 18.52667 19.76000 27.83833 32.87000 21.42167 24.70833 -1.6 1.6 20.08500 24.12000 PROBLEM 1 -- How do I sort nr.carbon such that 1.2 comes before -1.2, 1.3 comes before -1.3, etc. PROBLEM 2 -- How do I get rid of the pesky names automatically assigned by sapply so that I only have the data in nr.carbon? Thanks in advance to those who reply! -------------------------------------- Philip Turk Assistant Professor of Applied Statistics Department of Mathematical Sciences University of Alaska Anchorage CAS #154N, 3211 Providence Drive Anchorage, Alaska 99508 Phone: 907-786-1163 Fax: 907-786-6162 Email: afpjt at uaa.alaska.edu Web Page: http://afpjt.uaa.alaska.edu
Andrew Robinson
2005-Jan-03 23:58 UTC
[R] Two brief questions concerning sapply. Can anyone please help?!
Philip, 1) right now, tillage is being defined as a numeric vector and then being coerced into a factor. So, try defining tillage in this way: tillage <- factor(rep(c("1","-1"), each = 6, times = 5), levels=c("1","-1")) 2) Why do you want to do this? I hope this helps, Andrew On Mon, Jan 03, 2005 at 04:38:10PM -0700, afpjt at uaa.alaska.edu wrote:> To anyone who can help: > > I have two brief questions concerning sapply. Following below is the > code for my example. The two problems are described at the end of the > code: > > > site <- rep(2:6, each = 12) > > tillage <- rep(c(1,-1), each = 6, times = 5) > > carbon <- c(18.23, 16.06, 17.81, 16.07, 17.26, 17.08, > 14.92, 15.88, 12.11, 14.23, 16.99, 13.57, > 20.34, 20.3, 18.78, 19.91, 18.43, 20.8, > 18.98, 19.24, 18.18, 16.69, 19.72, 18.35, > 36.83, 32.85, 30.21, 31.58, 34.35, 31.4, > 27.72, 29.9, 25.97, 27.63, 28.13, 27.68, > 23.11, 24.32, 21.87, 29.18, 26.52, 23.25, > 24.16, 17.62, 19.62, 20.5, 21.01, 25.62, > 25.73, 23.42, 25.17, 23.76, 23.27, 23.37, > 20.89, 19.93, 19.24, 19.4, 22.09, 18.96) > > junk <- interaction(tillage, site) > > nr.carbon <- sapply(split(carbon, junk), function(x) mean(x)) > > > nr.carbon > > -1.2 1.2 -1.3 1.3 -1.4 1.4 -1.5 1.5 > 14.61667 17.08500 18.52667 19.76000 27.83833 32.87000 21.42167 24.70833 > -1.6 1.6 > 20.08500 24.12000 > > PROBLEM 1 -- How do I sort nr.carbon such that 1.2 comes before -1.2, > 1.3 comes before -1.3, etc. > > PROBLEM 2 -- How do I get rid of the pesky names automatically assigned > by sapply so that I only have the data in nr.carbon? > > Thanks in advance to those who reply! > > -------------------------------------- > Philip Turk > Assistant Professor of Applied Statistics > Department of Mathematical Sciences > University of Alaska Anchorage > CAS #154N, 3211 Providence Drive > Anchorage, Alaska 99508 > Phone: 907-786-1163 > Fax: 907-786-6162 > Email: afpjt at uaa.alaska.edu > Web Page: http://afpjt.uaa.alaska.edu > > ______________________________________________ > R-help at 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-- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : andrewr at uidaho.edu PO Box 441133 W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion.
Liaw, Andy
2005-Jan-04 00:42 UTC
[R] Two brief questions concerning sapply. Can anyone please help?!
Try something like:> nr.carbon.2 <- tapply(carbon, list(tillage, site), mean) > nr.carbon.22 3 4 5 6 -1 14.61667 18.52667 27.83833 21.42167 20.085 1 17.08500 19.76000 32.87000 24.70833 24.120> as.vector(nr.carbon.2[2:1,])[1] 17.08500 14.61667 19.76000 18.52667 32.87000 27.83833 24.70833 21.42167 [9] 24.12000 20.08500 Andy> From: afpjt at uaa.alaska.edu > > To anyone who can help: > > I have two brief questions concerning sapply. Following below is the > code for my example. The two problems are described at the > end of the > code: > > > site <- rep(2:6, each = 12) > > tillage <- rep(c(1,-1), each = 6, times = 5) > > carbon <- c(18.23, 16.06, 17.81, 16.07, 17.26, 17.08, > 14.92, 15.88, 12.11, 14.23, 16.99, 13.57, > 20.34, 20.3, 18.78, 19.91, 18.43, 20.8, > 18.98, 19.24, 18.18, 16.69, 19.72, 18.35, > 36.83, 32.85, 30.21, 31.58, 34.35, 31.4, > 27.72, 29.9, 25.97, 27.63, 28.13, 27.68, > 23.11, 24.32, 21.87, 29.18, 26.52, 23.25, > 24.16, 17.62, 19.62, 20.5, 21.01, 25.62, > 25.73, 23.42, 25.17, 23.76, 23.27, 23.37, > 20.89, 19.93, 19.24, 19.4, 22.09, 18.96) > > junk <- interaction(tillage, site) > > nr.carbon <- sapply(split(carbon, junk), function(x) mean(x)) > > > nr.carbon > > -1.2 1.2 -1.3 1.3 -1.4 1.4 > -1.5 1.5 > 14.61667 17.08500 18.52667 19.76000 27.83833 32.87000 > 21.42167 24.70833 > -1.6 1.6 > 20.08500 24.12000 > > PROBLEM 1 -- How do I sort nr.carbon such that 1.2 comes before -1.2, > 1.3 comes before -1.3, etc. > > PROBLEM 2 -- How do I get rid of the pesky names > automatically assigned > by sapply so that I only have the data in nr.carbon? > > Thanks in advance to those who reply! > > -------------------------------------- > Philip Turk > Assistant Professor of Applied Statistics > Department of Mathematical Sciences > University of Alaska Anchorage > CAS #154N, 3211 Providence Drive > Anchorage, Alaska 99508 > Phone: 907-786-1163 > Fax: 907-786-6162 > Email: afpjt at uaa.alaska.edu > Web Page: http://afpjt.uaa.alaska.edu > > ______________________________________________ > R-help at 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 > >