使用和mapply在一起

时间:2016-01-12 18:31:49

标签: r mapply

您好我正试图让ICC的价值以一组因子水平为条件。例如:

usr1<-data.frame(a1=1:5,a2=11:15,a3=21:25,bin=factor(c("a","b","a","a","b")))
usr2<-data.frame(a1=2:6,a2=12:16,a3=32:36)

我想要a1,a2和a3的ICC,其中bin是a和b。在调整因子之前,我使用了mapply

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[,-4],usr2)

我无法弄清楚如何为每个级别的因素包装它。

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[c(1,3,4),-4],usr2[c(1,3,4),])

应该为您提供“a”级别的正确答案 和

mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[c(2,5),-4],usr2[c(2,5),])

应该为您提供“b”等级的正确答案。

这是我的问题的一个非常简化的版本,我必须计算超过200个ICC的20个级别。我事先并不知道哪些行有哪些因素,除了我在data.frame中有这个因素 谢谢

修改

预期的输出就像....

usr1$bin:a
           a1          a2          a3          
subjects   3           3           3           
raters     2           2           2           
model      "twoway"    "twoway"    "twoway"    
type       "agreement" "agreement" "agreement" 
unit       "single"    "single"    "single"    
icc.name   "ICC(A,1)"  "ICC(A,1)"  "ICC(A,1)"  
value      0.8235294   0.8235294   0.03713528  
r0         0           0           0           
Fvalue     -5.2542e+15 -5.2542e+15 1.094625e+14
df1        2           2           2           
df2        1           1           1           
p.value    1           1           6.758531e-08
conf.level 0.95        0.95        0.95        
lbound     0.005803109 0.005803109 4.823719e-05
ubound     0.9944658   0.9944658   0.5976005   
------------------------------------------------------
usr1$bin:b
           a1          a2          a3          
subjects   2           2           2           
raters     2           2           2           
model      "twoway"    "twoway"    "twoway"    
type       "agreement" "agreement" "agreement" 
unit       "single"    "single"    "single"    
icc.name   "ICC(A,1)"  "ICC(A,1)"  "ICC(A,1)"  
value      0.9         0.9         0.06923077  
r0         0           0           0           
Fvalue     Inf         Inf         Inf         
df1        1           1           1           
df2        1           1           1           
p.value    0           0           0           
conf.level 0.95        0.95        0.95        
lbound     0.01370303  0.01370303  0.0001148084
ubound     0.9998285   0.9998285   0.9796676   

1 个答案:

答案 0 :(得分:2)

您可以为lapply

包装bin
res<-lapply(unique(usr1$bin),function(x){  
  mapply(function(x,y){z<-data.frame(x,y);icc(z,model="t",type="a")},usr1[usr1$bin==x,-4],usr2[usr1$bin==x,])
})
names(res)<-unique(usr1$bin)