[R] Two brief questions concerning sapply. Can anyone please help?!

Andrew Robinson andrewr at uidaho.edu
Tue Jan 4 00:58:13 CET 2005


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.




More information about the R-help mailing list