我使用波纹管功能将组级别与 pairwise.wilcox.test 进行比较:
pairwise.wilcox <- function(data, x, factor) {
require(reshape2)
pw <- data.frame()
for(i in x){
pw <- pairwise.wilcox.test(data[ ,i], factor, p.adj = "bonf")
#pairwise.wilcox.test output is reformatted
pw <- melt(pw[[3]])
#the first two columns of the reformatted output are merged
pw$Var1 = paste(pw$Var2, pw$Var1, sep=" - ")
#the second column is dropped
drops <- c("Var2")
pw <- pw[,!(names(pw) %in% drops)]
colnames(pw) <- c("Compared groups", "p-value")
print(pw)
}
}
pairwise.wilcox(tabel2, c(2:4), tabel2$lot)
这是输出的摘录:
Compared groups p-value
1 1StL - 3StL 1
2 1StL - IP 1
3 1StL - ISR 1
4 1StL - ISU 1
5 1StL - StM 1
6 3StL - 3StL NA
7 3StL - IP 1
...
Compared groups p-value
1 1StL - 3StL 1,0000000
2 1StL - IP 0,1092484
3 1StL - ISR 1,0000000
4 1StL - ISU 1,0000000
5 1StL - StM 1,0000000
6 3StL - 3StL NA
7 3StL - IP 1,0000000
...
Compared groups p-value
1 1StL - 3StL 1
2 1StL - IP 1
3 1StL - ISR 1
4 1StL - ISU 1
5 1StL - StM 1
6 3StL - 3StL NA
7 3StL - IP 1
...
我想要的是通过公共第一列水平连接这些数据帧,因此结果输出如下所示:
Compared groups p-value p-value p-value
1 1StL - 3StL 1 1,0000000 1
2 1StL - IP 1 0,1092484 1
3 1StL - ISR 1 1,0000000 1
4 1StL - ISU 1 1,0000000 1
5 1StL - StM 1 1,0000000 1
6 3StL - 3StL NA NA NA
7 3StL - IP 1 1,0000000 1
...
怎么做?请记住,我有更多变量。
我的数据集(不是所有数据):
structure(list(lot = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L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2, 3, 1, 3, 2,
3, 3, 3, 2, 3, 3, 2, 3, 2, 3, 2, 3, 1, 2, 3, 3, 3, 1, 2, 3, 2,
1, 3, 2, 1, 1, 3, 1, 1, 1, 1, 3, 3, 1, 3, 3, 3, 2, 3, 1, 3, 3,
3, 1, 3, 2, 3, 2, 1, 1, 2, 1, 3, 3, 3, 3, 3, 3, 3, 1, 2, 3, 1,
1, 3, 1, 1, 3, 3, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 2, 2, 1, 2, 2,
1, 1, 3, 1, 1, 3, 3, 2, 1, 1)), .Names = c("lot", "RS.5_1", "RS.5_2",
"RS.5_3"), row.names = c(NA, -582L), class = "data.frame")
答案 0 :(得分:2)
不确定这会增加多少,但您询问如何在函数中实现它:
pw.wcx <- function(data,x,factor){
result <- Reduce(function(x,y)merge(x,y,by="Var1"),
lapply(data[x],function(d){
pw <- pairwise.wilcox.test(d,factor,p.adj="bonf")[[3]]
transform(melt(pw),Var1=paste(Var2,Var1,sep="-"))[-2]}),
accumulate=FALSE)
colnames(result) <- c("Compared Groups",names(data[x]))
result
}
result <- pw.wcx(tabel2,2:4,tabel2$lot)
head(result)
# Compared Groups RS.5_1 RS.5_2 RS.5_3
# 1 1StL-3StL 1 1.0000000 1
# 2 1StL-IP 1 0.1092484 1
# 3 1StL-ISR 1 1.0000000 1
# 4 1StL-ISU 1 1.0000000 1
# 5 1StL-StM 1 1.0000000 1
# 6 3StL-3StL NA NA NA
因此,这会使用以下内容生成数据框列表:
lapply(data[x],function(d){
pw <- pairwise.wilcox.test(d,factor,p.adj="bonf")[[3]]
transform(melt(pw),Var1=paste(Var2,Var1,sep="-"))[-2]})
然后使用Reduce(...)
将它们组合起来,就像在另一个答案中一样。
编辑:回应OP的评论。
pw.wcx <- function(data,x,factor){
result <- Reduce(function(x,y)merge(x,y,by="Var1"),
lapply(x,function(i){
pw <- pairwise.wilcox.test(data[,i],factor,p.adj="bonf")[[3]]
transform(melt(pw,value.name=names(data)[i]),Var1=paste(Var2,Var1,sep="-"))[-2]}),
accumulate=FALSE)
colnames(result)[1] <- "Compared Groups"
result
}
此版本将避免警告,但我不推荐它。这基本上使用lapply(...)
作为一个美化的for循环,它没有利用R非常有效的列表处理(例如,数据帧使用data[,i]
每次迭代进行一次子集化而不仅仅是一旦开始,使用data[x]
。
答案 1 :(得分:1)
你的结构声明对我不起作用......我认为这是你问题的本质?
df1 <- data.frame(group=c('A', 'B', 'C'), result1=c(1, 2, 3))
df2 <- data.frame(group=c('A', 'B', 'C'), result2=c(4, 5, 6))
df3 <- data.frame(group=c('A', 'B', 'C'), result3=c(7, 8, 9))
result.temp <- merge(df1, df2)
result <- merge(result.temp, df3)
result
# group result1 result2 result3
# 1 A 1 4 7
# 2 B 2 5 8
# 3 C 3 6 9
否则,如果它们的长度始终相同,为什么不只是cbind
?
Per @ Roland在评论中的建议:
df.list <- list(df1, df2, df3)
Reduce(function(x, y) merge(x, y, all=T, by=c("group")), df.list, accumulate=F)