【问题标题】:using by and mapply together一起使用 by 和 mapply
【发布时间】:2016-04-17 12:24:24
【问题描述】:

您好,我正在尝试以一组因子水平为条件获取 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)

我不知道如何用 by 包装它来为因子的每个级别执行此操作。

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”级的正确答案。

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

编辑

预期的输出应该是......

usr1$bin:a a1 a2 a3 科目 3 3 3 评分员 2 2 2 型号 "twoway" "twoway" "twoway" 键入“协议”“协议”“协议” 单位“单”“单”“单” icc.name "ICC(A,1)" "ICC(A,1)" "ICC(A,1)" 值 0.8235294 0.8235294 0.03713528 r0 0 0 0 F值 -5.2542e+15 -5.2542e+15 1.094625e+14 df1 2 2 2 df2 1 1 1 p.value 1 1 6.758531e-08 配置级别 0.95 0.95 0.95 绑定 0.005803109 0.005803109 4.823719e-05 未绑定 0.9944658 0.9944658 0.5976005 -------------------------------------------------- ---- usr1$bin:b a1 a2 a3 科目 2 2 2 评分员 2 2 2 型号 "twoway" "twoway" "twoway" 键入“协议”“协议”“协议” 单位“单”“单”“单” icc.name "ICC(A,1)" "ICC(A,1)" "ICC(A,1)" 值 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 配置级别 0.95 0.95 0.95 绑定 0.01370303 0.01370303 0.0001148084 未绑定 0.9998285 0.9998285 0.9796676

【问题讨论】:

  • 你能显示预期的输出吗

标签: r mapply


【解决方案1】:

您可以将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)

【讨论】:

  • 感谢@fishtank 我一直坚持使用我没想到的 lapply。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
相关资源
最近更新 更多