我一直在尝试调试这个过去2天,应用我在Stack Overflow上找到的所有可能的修复,但我仍然遇到各种错误,我不知道我能做什么。< / p>
dat 是一个包含3051行和38列的数据框,取自多测试库中的golub数据集。 dat样本:
> 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 给我一个新错误 x [s1]中的错误:只有0可能与负下标混合< /强>
有趣的是,我在某些时候也遇到了错误 x [s1,]错误:维度错误运行同一行代码(“未使用的参数”不< / em>从wilcox.test中删除了,但那是2天前我无法再次重现它。
我也尝试了lapply()和mapply(),但是我得到了相同的未使用的参数错误。
我想要实现的目标:wilcox.test(),如果我正确理解问题,应该应用于x向量由第1列到第28列和y向量列29到38组成的每一行。
如果这是一个我错过的愚蠢的简单问题,我道歉。我只是不知道它是什么:(
编辑:重新启动R后,现在可以使用(以及Parfait的代码)...对不起,这应该是我在发布之前先尝试过的...
答案 0 :(得分:1)
考虑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