【发布时间】:2020-10-01 11:38:14
【问题描述】:
我有一个包含 6 列的数据框。前 4 列各包含 2 个因子。
我想编写一个函数(或 for 循环)来在每列的因子之间针对 pc1 和 pc2 列的值执行测试(例如 wilcox.test)。
如果我要手动操作:
wilcox.test(df[df$g1=="bm",5],df[df$g1!="bm",5])
wilcox.test(df[df$g1=="bm",6],df[df$g1!="bm",6])
我怎样才能得到每个测试的p.values 存储在一个数据框中,其中rows 等于df 和columns 的前4 列等于pc1 和pc2。
我试过了,但不正确:
mutate_if(df[,head(colnames(df),-2)], is.character, as.factor) %>% #check whether 4 first columns are as factor
lapply(.,
function(x) {
df = data.frame(row.names = head(colnames(df),-2))
names(df) = c("pc1", "pc2")
df$pc1 = wilcox.test(df[df$g1=="bm",5],df[df$g1!="bm",5])
df$pc2 = wilcox.test(df[df$g1=="bm",6],df[df$g1!="bm",6])
return(df)
}
)
我的数据框
> dput(df)
structure(list(g1 = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L,
1L, 1L), .Label = c("bm", "cm"), class = "factor"), g2 = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("ct", "ft"), class = "factor"),
g3 = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("bn",
"un"), class = "factor"), g4 = structure(c(2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L), .Label = c("ls", "vp"), class = "factor"),
pc1 = c(0.86, 0.54, 0.06, 0.88, 0.62, 0.14, 0.94, 0.8, 0.34,
0.04), pc2 = c(0.04, 0.9, 0.68, 0.54, 0.92, 0.36, 0.3, 0.62,
0.84, 0.96)), class = "data.frame", row.names = c(NA, -10L
))
【问题讨论】: