【问题标题】:Running loop on groups of data frame in R在R中的数据帧组上运行循环
【发布时间】:2016-08-24 00:43:06
【问题描述】:

我需要编写一个循环,该循环将在一小部分数据帧上运行t-tests。我认为他们建议使用for loop。 数据框中有 271 行。前 260 行需要分成 13 组,每组 20 行,并且必须在 13 组中的每一个上运行 t-test

这是我用来在整个数据框上运行t-test 的代码:

t.test(a, c, alternative =c("two.sided"), mu=0, paired=TRUE, var.equal=TRUE, conf.level=0.95)

我是一个编码新手,请帮助! D:

【问题讨论】:

    标签: r loops statistics


    【解决方案1】:

    首先,我在这里看不到 data.frame。 ac 似乎是向量。我假设这两个向量的长度为 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 代表数据框中的列 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2019-07-29
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多