[R] mapply on multiple data frames
arun
smartpink111 at yahoo.com
Tue Jun 11 09:29:16 CEST 2013
Hi,
It would be better to provide a reproducible example.
set.seed(25)
all_dfs<- list(df1=data.frame(col1=sample(1:40,20,replace=TRUE),col2=sample(20:40,20,replace=TRUE),col3= sample(1:3,20,replace=TRUE)),df2=data.frame(col1=sample(30:60,20,replace=TRUE),col2=sample(35:65,20,replace=TRUE),col3=sample(1:3,20,replace=TRUE)),df3=data.frame(col1=sample(40:80,20,replace=TRUE),col2=sample(25:45,20,replace=TRUE),col3=sample(1:3,20,replace=TRUE)))
all_nums<-as.list(1:3)
f<- function(x,y){
out<-t.test(x$col1[x$col3==y],x$col2[x$col3==y],paired=T,alternative="greater")$p.value
out
}
mapply(f,all_dfs,all_nums)
# df1 df2 df3
#0.926400219 0.713943295 0.004510331
##Just for validation:
df1New<-all_dfs[[1]]
df2New<-all_dfs[[2]]
df3New<- all_dfs[[3]]
t.test(df1New$col1[df1New$col3==1],df1New$col2[df1New$col3==1],paired=T,alternative="greater")$p.value
#[1] 0.9264002
t.test(df2New$col1[df2New$col3==2],df2New$col2[df2New$col3==2],paired=T,alternative="greater")$p.value
#[1] 0.7139433
t.test(df3New$col1[df3New$col3==3],df3New$col2[df3New$col3==3],paired=T,alternative="greater")$p.value
#[1] 0.004510331
A.K.
----- Original Message -----
From: "Webb,Elizabeth E" <webbe at ufl.edu>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc:
Sent: Tuesday, June 11, 2013 2:07 AM
Subject: [R] mapply on multiple data frames
Hi all-
I am wondering about using the mapply function to multiple data frames. Specifically, I would like to do a t-test on a subset of multiple data frames. All data frames have the same structure.
Here is my code so far:
f<-function(x,y) {
test<-t.test(x$col1[x$col3=="num",],v$col2[x$col3=="num",],paired=T,alternative="greater")
out<-test$p.value
return(out)
}
all_nums<-list(num1,num2,num3,num4)
all_dfs<-list(df1,df2,df3,df4)
mapply(f,all_dfs,all_nums)
This tells me that $ operator is invalid for atomic vectors. I have tried shifting to notation using [ ,] to denote columns, but that gives me this error: incorrect number of dimensions.
Thank you in advance,
Elizabeth
______________________________________________
R-help at r-project.org 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.
More information about the R-help
mailing list