R 使用连续分类器拆分应用函数的输入
我有一个示例数据框R 使用连续分类器拆分应用函数的输入,r,plyr,apply,reshape,R,Plyr,Apply,Reshape,我有一个示例数据框 test.df<-data.frame(classifier=runif(n=1000), x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000)) test.df这就是你想要的吗 df<-data.frame(classifier=runif(n=1000), x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000)) sapply(df[,-1], function(v) { t.
test.df<-data.frame(classifier=runif(n=1000), x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000))
test.df这就是你想要的吗
df<-data.frame(classifier=runif(n=1000), x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000))
sapply(df[,-1], function(v) {
t.test(v[df$classifier<0.4], v[df$classifier>0.6])$p.value
})
这是你想要的吗
df<-data.frame(classifier=runif(n=1000), x1=rnorm(1000), x2=rnorm(1000), x3=rnorm(1000))
sapply(df[,-1], function(v) {
t.test(v[df$classifier<0.4], v[df$classifier>0.6])$p.value
})
使用应用
:
idx <- matrix(2:ncol(test.df), ncol=1)
apply(idx, 1, function(x) {
v1 <- test.df[test.df$classifier < 0.4, x]
v2 <- test.df[test.df$classifier > 0.6, x]
t.test(v1, v2)$p.value
})
idx使用apply
:
idx <- matrix(2:ncol(test.df), ncol=1)
apply(idx, 1, function(x) {
v1 <- test.df[test.df$classifier < 0.4, x]
v2 <- test.df[test.df$classifier > 0.6, x]
t.test(v1, v2)$p.value
})
idx它是一个t.test
还是一个示例?我的原始函数是一个自定义函数,但它的参数类似于t.test
。例如,您可以使用t.test
。它是t.test
还是仅仅是一个示例?我的原始函数是一个自定义函数,但其参数与t.test
类似。例如,您可以使用t.test
。Yes。就这样。为什么选择了sapply
而不是apply
。出于好奇,是否可以使用apply
?@ECII,sapply
执行相同的操作,因为data.frame在内部是一个列表,可以直接通过sapply
传递列。请检查我的答案,了解如何使用apply
,虽然sapply
是您应该使用的。@juba,(+1),但我怀疑sapply(2:ncol(df),function(idx)t.test(df[[idx]]][df[[1]]<0.4],df[[idx]]][df[[1]>0.6])$p.value)
会更快(如果OP不想知道名字的话)。是的。就这样。为什么选择了sapply
而不是apply
。出于好奇,是否可以使用apply
?@ECII,sapply
执行相同的操作,因为data.frame在内部是一个列表,可以直接通过sapply
传递列。请检查我的答案,了解如何使用apply
,虽然sapply
是您应该使用的。@juba,(+1),但我怀疑sapply(2:ncol(df),function(idx)t.test(df[[idx]]][df[[1]]<0.4],df[[idx]]][df[[1]>0.6])$p.value)
会更快(如果OP不需要名字的话)。