White, Charles E WRAIR-Wash DC
2003-May-30 19:38 UTC
[R] Extracting Vectors from Lists of Lists Produced by Functions
If you found my subject heading to be confusing then I'm sure you'll enjoy the example I've included below. I find the apply type functions to be wonderful for avoiding loops but when I use them with existing functions, I end up using loops anyway to extract the vectors I want. I would appreciate it if someone could show me how to avoid these loops. Thanks. EXAMPLE: noise<-matrix(data = rnorm(15, mean=0, sd=1), nrow = 5, ncol = 3, byrow = FALSE, dimnames = NULL) measure<-apply(noise,2,t.test) measure tval<-NULL df<-NULL pval<-NULL for (i in 1:length(measure)){ tval[i]<-measure[[i]][[1]] df[i]<-measure[[i]][[2]] pval[i]<-measure[[i]][[3]]} data.frame(tval,df,pval) Charles E. White, Biostatistician Walter Reed Army Institute of Research 503 Robert Grant Ave., Room 1w102 Silver Spring, MD 20910-1557 301 319-9781 WRAIR Home Page: http://wrair-www.army.mil/
Sundar Dorai-Raj
2003-May-30 19:53 UTC
[R] Extracting Vectors from Lists of Lists Produced by Functions
White, Charles E WRAIR-Wash DC wrote:> If you found my subject heading to be confusing then I'm sure you'll enjoy > the example I've included below. I find the apply type functions to be > wonderful for avoiding loops but when I use them with existing functions, I > end up using loops anyway to extract the vectors I want. I would appreciate > it if someone could show me how to avoid these loops. Thanks. > > EXAMPLE: > noise<-matrix(data = rnorm(15, mean=0, sd=1), nrow = 5, ncol = 3, > byrow = FALSE, dimnames = NULL) > measure<-apply(noise,2,t.test) > measure > tval<-NULL > df<-NULL > pval<-NULL > for (i in 1:length(measure)){ > tval[i]<-measure[[i]][[1]] > df[i]<-measure[[i]][[2]] > pval[i]<-measure[[i]][[3]]} > data.frame(tval,df,pval) > > Charles E. White, Biostatistician > Walter Reed Army Institute of Research > 503 Robert Grant Ave., Room 1w102 > Silver Spring, MD 20910-1557 > 301 319-9781 > WRAIR Home Page: http://wrair-www.army.mil/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Charles, Use a data.frame and sapply instead. sapply(as.data.frame(noise), function(x) t.test(x)[1:3]) Sundar
J.R. Lockwood
2003-May-30 19:54 UTC
[R] Extracting Vectors from Lists of Lists Produced by Functions
Dear Charles, Since you are extracting vectors of the same length from each element of the list, you can use "sapply" "sapply(measure,function(x){x[1:3]})" after which you can transpose, rename, make into a dataframe as desired. In general you would use "lapply" to apply a function to each element of a list, resulting in new list. best, J.R. Lockwood 412-683-2300 x4941 lockwood at rand.org http://www.rand.org/methodology/stat/members/lockwood/> If you found my subject heading to be confusing then I'm sure you'll enjoy > the example I've included below. I find the apply type functions to be > wonderful for avoiding loops but when I use them with existing functions, I > end up using loops anyway to extract the vectors I want. I would appreciate > it if someone could show me how to avoid these loops. Thanks. > > EXAMPLE: > noise<-matrix(data = rnorm(15, mean=0, sd=1), nrow = 5, ncol = 3, > byrow = FALSE, dimnames = NULL) > measure<-apply(noise,2,t.test) > measure > tval<-NULL > df<-NULL > pval<-NULL > for (i in 1:length(measure)){ > tval[i]<-measure[[i]][[1]] > df[i]<-measure[[i]][[2]] > pval[i]<-measure[[i]][[3]]} > data.frame(tval,df,pval) > > Charles E. White, Biostatistician > Walter Reed Army Institute of Research > 503 Robert Grant Ave., Room 1w102 > Silver Spring, MD 20910-1557 > 301 319-9781 > WRAIR Home Page: http://wrair-www.army.mil/ >
Possibly Parallel Threads
- Compiling errors for Matrix_0.995-5.tar.gz under XP
- probit analysis for correlated binary data
- lme4_0.995-2/Matrix_0.995-4 upgrade introduces error messages (change management)
- WinXP "developer" asks: Tcl/Tk (Rcmdr) under OS X?
- Menu addditions to Rcmdr v0.9-6