首先,我在这里看不到 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]] # ...
我希望能回答你的问题。