我有多个数据框,每个数据框都有不同样本的年龄列。以下数据框只有感兴趣的列(年龄)
df1 <- c(34, 25, 45, 6, 67, 12)
df2 <- c(31, 23, 71, 19)
df3 <- c(12, 45, 42, 89, 12)
df4 <- c(34, 37, 23)如果我运行
results <- dunn.test(list(df1, df2, df3, df4))它输出6个可能比较中的每一个的p值。只有当其中一个p值小于.05时,我才能让它输出它来自哪个特定的数据帧(例如,df1和df2比较的p值为.01,所以我希望它输出df1,df2)。它在结果数据框中输出“比较”,但如果我能得到它所来自的特定数据框就更好了。
我正在处理的数据是许多类似于list(df1,df2,df3,df4)的列表,所以我正在寻找一个通用的解决方案。
如果你有什么需要澄清的,请提出来!
发布于 2017-08-17 03:08:18
有几种方法可以做到这一点。
为数据框命名
df <- list("a" = df1, "b" = df2, "c" = df3, "d" = df4)然后使用?sapply()
result <- sapply(df, function(x){
dunn.test(x)
})查看result的属性
str(result)
List of 20
$ : num 5
$ : num [1:15] 0.378 -0.378 -0.756 1.134 0.756 ...
$ : num [1:15] 0.353 0.353 0.225 0.128 0.225 ...
$ : num [1:15] 0.353 0.353 0.225 0.128 0.225 ...
$ : chr [1:15] "1 - 2" "1 - 3" "2 - 3" "1 - 4" ...
$ : num 3
$ : num [1:6] 0.548 -0.548 -1.095 1.095 0.548 ...
$ : num [1:6] 0.292 0.292 0.137 0.137 0.292 ...
$ : num [1:6] 0.292 0.292 0.137 0.137 0.292 ...
$ : chr [1:6] "1 - 2" "1 - 3" "2 - 3" "1 - 4" ...
$ : num 4
$ : num [1:10] -1.147 -0.688 0.459 -1.606 -0.459 ...
$ : num [1:10] 0.1257 0.2456 0.3232 0.0541 0.3232 ...
$ : num [1:10] 0.1257 0.2456 0.3232 0.0541 0.3232 ...
$ : chr [1:10] "1 - 2" "1 - 3" "2 - 3" "1 - 4" ...
$ : num 2
$ : num [1:3] -0.707 0.707 1.414
$ : num [1:3] 0.2398 0.2398 0.0786
$ : num [1:3] 0.2398 0.2398 0.0786
$ : chr [1:3] "1 - 2" "1 - 3" "2 - 3"
- attr(*, "dim")= int [1:2] 5 4
- attr(*, "dimnames")=List of 2
..$ : chr [1:5] "chi2" "Z" "P" "P.adjusted" ...
..$ : chr [1:4] "a" "b" "c" "d"那么去吧
值标签的attr(result,"dimnames")[1]或
变量名的attr(result,"dimnames")[2]
https://stackoverflow.com/questions/45720938
复制相似问题