首先,我没有在这里看到data.frame。 a
和c
似乎是矢量。我假设这两个矢量的长度都是271,而你想忽略最后的11个项目。所以你可以先扔掉这些东西:
a2 <- a[1:260]
c2 <- c[1:260]
现在你可以创建一个长度为260的矢量来确定子集的索引。 (有很多方法可以做到这一点,但我觉得这种方式很容易理解。)
indices <- as.numeric(cut(1:260, 20))
indices #just to show the output
你可能要存储在列表输出。下面的代码再次不是最高效的,但容易理解。
result <- list()
for (i in 1:20){
result[[i]] <- t.test(a2[which(indices == i)], c2[which(indices == i)],
alternative = c("two.sided"),
mu = 0, paired = TRUE, var.equal = TRUE,
conf.level = 0.95)
}
result[[1]] #gives the results of the first t-test (items 1 to 20)
result[[2]] # ...
作为替代for
-loop你也可以使用lapply
这通常是更有效和更短些(但是不要紧,260个数据点):
result2 <- lapply(1:20, function(i) t.test(a2[which(indices == i)],
c2[which(indices == i)],
alternative = c("two.sided"),
mu = 0, paired = TRUE, var.equal = TRUE,
conf.level = 0.95))
result[[1]] # ...
希望这是你的问题。
嘿,对不起,我认为数据帧太大而且杂乱。 A和c代表数据帧中的列 非常感谢您的帮助! – Emilia