如何按 R 中的组对所有连续的列对进行测试
How to do a test to all sequential pairs of columns by groups in R
我正在尝试将 wilcoxon test
应用于 df
的所有顺序列对。我使用 col<-seq(2,ncol(df),by=2)
.
没问题
但我不知道如何分组,在这种情况下 skul
。
library(tidyverse)
df <- tibble(skul = c(rep('a',15), rep('b', 16)),
x1i = sample(1:10, 31, replace = TRUE),
x1f = sample(1:10, 31, replace = TRUE),
x2i = sample(1:10, 31, replace = TRUE),
x2f = sample(1:10, 31, replace = TRUE))
col<-seq(2,ncol(df),by=2)
# I have to select out the column "skul"
map2(df[,col], df[,c(-col, -1)], wilcox.test)
#> Warning in wilcox.test.default(.x[[i]], .y[[i]], ...): cannot compute exact p-
#> value with ties
#> Warning in wilcox.test.default(.x[[i]], .y[[i]], ...): cannot compute exact p-
#> value with ties
#> $x1i
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: .x[[i]] and .y[[i]]
#> W = 486, p-value = 0.9435
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> $x2i
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: .x[[i]] and .y[[i]]
#> W = 401, p-value = 0.2613
#> alternative hypothesis: true location shift is not equal to 0
由 reprex 包 (v2.0.1) 创建于 2022-04-13
这是您要找的吗:
df <- dplyr::tibble(skul = c(rep('a',15), rep('b', 16)),
x1i = sample(1:10, 31, replace = TRUE),
x1f = sample(1:10, 31, replace = TRUE),
x2i = sample(1:10, 31, replace = TRUE),
x2f = sample(1:10, 31, replace = TRUE))
stems <- unique(gsub("[if]", "", names(df)[-1]))
out <- by(df, list(df$skul), function(data){
lapply(stems, function(x){
inds <- grep(x, names(df))
n <- names(df)[inds]
cmd <- paste0("wilcox.test(data$", n[1], ", data$", n[2], ")")
eval(parse(text=cmd))
})})
#> Warning in wilcox.test.default(data$x1i, data$x1f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x2i, data$x2f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x1i, data$x1f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x2i, data$x2f): cannot compute exact p-value
#> with ties
out
#> : a
#> [[1]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x1i and data$x1f
#> W = 110, p-value = 0.9334
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> [[2]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x2i and data$x2f
#> W = 75.5, p-value = 0.1266
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> ------------------------------------------------------------
#> : b
#> [[1]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x1i and data$x1f
#> W = 113.5, p-value = 0.5952
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> [[2]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x2i and data$x2f
#> W = 152, p-value = 0.3719
#> alternative hypothesis: true location shift is not equal to 0
由 reprex package (v2.0.1)
于 2022-04-13 创建
我正在尝试将 wilcoxon test
应用于 df
的所有顺序列对。我使用 col<-seq(2,ncol(df),by=2)
.
但我不知道如何分组,在这种情况下 skul
。
library(tidyverse)
df <- tibble(skul = c(rep('a',15), rep('b', 16)),
x1i = sample(1:10, 31, replace = TRUE),
x1f = sample(1:10, 31, replace = TRUE),
x2i = sample(1:10, 31, replace = TRUE),
x2f = sample(1:10, 31, replace = TRUE))
col<-seq(2,ncol(df),by=2)
# I have to select out the column "skul"
map2(df[,col], df[,c(-col, -1)], wilcox.test)
#> Warning in wilcox.test.default(.x[[i]], .y[[i]], ...): cannot compute exact p-
#> value with ties
#> Warning in wilcox.test.default(.x[[i]], .y[[i]], ...): cannot compute exact p-
#> value with ties
#> $x1i
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: .x[[i]] and .y[[i]]
#> W = 486, p-value = 0.9435
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> $x2i
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: .x[[i]] and .y[[i]]
#> W = 401, p-value = 0.2613
#> alternative hypothesis: true location shift is not equal to 0
由 reprex 包 (v2.0.1) 创建于 2022-04-13
这是您要找的吗:
df <- dplyr::tibble(skul = c(rep('a',15), rep('b', 16)),
x1i = sample(1:10, 31, replace = TRUE),
x1f = sample(1:10, 31, replace = TRUE),
x2i = sample(1:10, 31, replace = TRUE),
x2f = sample(1:10, 31, replace = TRUE))
stems <- unique(gsub("[if]", "", names(df)[-1]))
out <- by(df, list(df$skul), function(data){
lapply(stems, function(x){
inds <- grep(x, names(df))
n <- names(df)[inds]
cmd <- paste0("wilcox.test(data$", n[1], ", data$", n[2], ")")
eval(parse(text=cmd))
})})
#> Warning in wilcox.test.default(data$x1i, data$x1f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x2i, data$x2f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x1i, data$x1f): cannot compute exact p-value
#> with ties
#> Warning in wilcox.test.default(data$x2i, data$x2f): cannot compute exact p-value
#> with ties
out
#> : a
#> [[1]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x1i and data$x1f
#> W = 110, p-value = 0.9334
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> [[2]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x2i and data$x2f
#> W = 75.5, p-value = 0.1266
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> ------------------------------------------------------------
#> : b
#> [[1]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x1i and data$x1f
#> W = 113.5, p-value = 0.5952
#> alternative hypothesis: true location shift is not equal to 0
#>
#>
#> [[2]]
#>
#> Wilcoxon rank sum test with continuity correction
#>
#> data: data$x2i and data$x2f
#> W = 152, p-value = 0.3719
#> alternative hypothesis: true location shift is not equal to 0
由 reprex package (v2.0.1)
于 2022-04-13 创建