R:使用 apply() 的不同错误
R: different errors from using apply()
过去 2 天我一直在尝试调试它,应用我在 Stack Overflow 上找到的所有可能的修复程序,但我仍然遇到各种错误,我不知道我能做什么.
dat是一个3051行38列的数据框,取自multtest库中的golub数据集。
数据样本:
> dat[1:5, 1:5]
V1 V2 V3 V4 V5
g1 -1.45769 -1.39420 -1.42779 -1.40715 -1.42668
g2 -0.75161 -1.26278 -0.09052 -0.99596 -1.24245
g3 0.45695 -0.09654 0.90325 -0.07194 0.03232
g4 3.13533 0.21415 2.08754 2.23467 0.93811
g5 2.76569 -1.27045 1.60433 1.53182 1.63728
我定义了这个函数:
> wilcox.func <- function(x, s1, s2) {
+ x1 <- x[s1]
+ x2 <- x[s2]
+ x1 <- as.numeric(x1)
+ x2 <- as.numeric(x2)
+ w.out <- wilcox.test(x1, x2, exact=F, alternative="two.sided", correct=T)
+ out <- as.numeric(w.out$statistic)
+ return(out) }
我尝试将其应用于:
> apply(dat, 1, wilcox.func, s1=c(1:27), s2=c(28:38))
我想 运行 wilcox.test() 函数,其中前 27 列为 x,其余列为 y(基于 golub.cl)。但是,我收到此错误:
Error in wilcox.test(x1, x2, exact = F, alternative = "two.sided", correct = T) :
unused arguments (exact = F, alternative = "two.sided", correct = T)
删除 exact = F, alternative = "two.sided", correct = T 给我一个新的错误 Error in x[s1] : only 0's可以混用负数下标.
有趣的是,在某些时候我还得到了错误 Error in x[s1, ] : incorrect number of dimensions 运行ning 同一行代码(与"unused arguments" not 从 wilcox.test 中删除),但那是 2 天前的事了,我无法再次复制它。
我也试过 lapply() 和 mapply(),但我得到了相同的未使用参数错误。
我想要实现的目标:wilcox.test(),如果我理解正确的话,应该应用于 x 向量由第 1 到 28 列和 y 向量组成的每一行第 29 至 38 列。
如果我遗漏了一个愚蠢的简单问题,我深表歉意。我只是不知道它是什么:(
编辑:重新启动 R 后现在可以使用(以及 Parfait 的代码)...抱歉,这应该是我在发布此之前首先尝试过的...
考虑 sapply()
或 vapply()
(预定义输出类型)遍历行号,因为您需要按每行的列范围进行切片。下面使用示例数据但调整为完整 .dat
:
# READ IN SAMPLE dat
data ='
V0 V1 V2 V3 V4 V5
g1 -1.45769 -1.39420 -1.42779 -1.40715 -1.42668
g2 -0.75161 -1.26278 -0.09052 -0.99596 -1.24245
g3 0.45695 -0.09654 0.90325 -0.07194 0.03232
g4 3.13533 0.21415 2.08754 2.23467 0.93811
g5 2.76569 -1.27045 1.60433 1.53182 1.63728'
dat <- read.table(text=data, header=TRUE, stringsAsFactors=FALSE)
# ADJUSTED FUNCTION
wilcox.func <- function(s1, s2) {
x1 <- as.numeric(s1)
x2 <- as.numeric(s2)
w.out <- wilcox.test(x1, x2, exact=F, alternative="two.sided", correct=T)
out <- as.numeric(w.out$statistic)
return(out)
}
output <- sapply(seq_len(nrow(dat)), function(i)
wilcox.func(dat[i, c(2:4)], dat[i, c(5:6)]))
output
# [1] 2 4 4 3 3
output <- vapply(seq_len(nrow(dat)), function(i)
wilcox.func(dat[i, c(2:4)], dat[i, c(5:6)]),
numeric(1))
output
# [1] 2 4 4 3 3
过去 2 天我一直在尝试调试它,应用我在 Stack Overflow 上找到的所有可能的修复程序,但我仍然遇到各种错误,我不知道我能做什么.
dat是一个3051行38列的数据框,取自multtest库中的golub数据集。 数据样本:
> dat[1:5, 1:5]
V1 V2 V3 V4 V5
g1 -1.45769 -1.39420 -1.42779 -1.40715 -1.42668
g2 -0.75161 -1.26278 -0.09052 -0.99596 -1.24245
g3 0.45695 -0.09654 0.90325 -0.07194 0.03232
g4 3.13533 0.21415 2.08754 2.23467 0.93811
g5 2.76569 -1.27045 1.60433 1.53182 1.63728
我定义了这个函数:
> wilcox.func <- function(x, s1, s2) {
+ x1 <- x[s1]
+ x2 <- x[s2]
+ x1 <- as.numeric(x1)
+ x2 <- as.numeric(x2)
+ w.out <- wilcox.test(x1, x2, exact=F, alternative="two.sided", correct=T)
+ out <- as.numeric(w.out$statistic)
+ return(out) }
我尝试将其应用于:
> apply(dat, 1, wilcox.func, s1=c(1:27), s2=c(28:38))
我想 运行 wilcox.test() 函数,其中前 27 列为 x,其余列为 y(基于 golub.cl)。但是,我收到此错误:
Error in wilcox.test(x1, x2, exact = F, alternative = "two.sided", correct = T) : unused arguments (exact = F, alternative = "two.sided", correct = T)
删除 exact = F, alternative = "two.sided", correct = T 给我一个新的错误 Error in x[s1] : only 0's可以混用负数下标.
有趣的是,在某些时候我还得到了错误 Error in x[s1, ] : incorrect number of dimensions 运行ning 同一行代码(与"unused arguments" not 从 wilcox.test 中删除),但那是 2 天前的事了,我无法再次复制它。
我也试过 lapply() 和 mapply(),但我得到了相同的未使用参数错误。
我想要实现的目标:wilcox.test(),如果我理解正确的话,应该应用于 x 向量由第 1 到 28 列和 y 向量组成的每一行第 29 至 38 列。
如果我遗漏了一个愚蠢的简单问题,我深表歉意。我只是不知道它是什么:(
编辑:重新启动 R 后现在可以使用(以及 Parfait 的代码)...抱歉,这应该是我在发布此之前首先尝试过的...
考虑 sapply()
或 vapply()
(预定义输出类型)遍历行号,因为您需要按每行的列范围进行切片。下面使用示例数据但调整为完整 .dat
:
# READ IN SAMPLE dat
data ='
V0 V1 V2 V3 V4 V5
g1 -1.45769 -1.39420 -1.42779 -1.40715 -1.42668
g2 -0.75161 -1.26278 -0.09052 -0.99596 -1.24245
g3 0.45695 -0.09654 0.90325 -0.07194 0.03232
g4 3.13533 0.21415 2.08754 2.23467 0.93811
g5 2.76569 -1.27045 1.60433 1.53182 1.63728'
dat <- read.table(text=data, header=TRUE, stringsAsFactors=FALSE)
# ADJUSTED FUNCTION
wilcox.func <- function(s1, s2) {
x1 <- as.numeric(s1)
x2 <- as.numeric(s2)
w.out <- wilcox.test(x1, x2, exact=F, alternative="two.sided", correct=T)
out <- as.numeric(w.out$statistic)
return(out)
}
output <- sapply(seq_len(nrow(dat)), function(i)
wilcox.func(dat[i, c(2:4)], dat[i, c(5:6)]))
output
# [1] 2 4 4 3 3
output <- vapply(seq_len(nrow(dat)), function(i)
wilcox.func(dat[i, c(2:4)], dat[i, c(5:6)]),
numeric(1))
output
# [1] 2 4 4 3 3