【问题标题】:Creating a t-test loop over a dataframe using an index使用索引在数据帧上创建 t 检验循环
【发布时间】:2019-08-29 12:21:52
【问题描述】:

所以,假设我有一个 1000 行、6 列的数据框,列是 a1、a2、b1、b2、c1、c2。我想使用 a's、b's 和 c's 运行一些 t 检验,并获得一个输出 df,其中 3 列用于 a-b-c 的 t 值,另外 3 列用于这些值的显着性信息,总共 6 列。我遇到的问题是行,我想循环 20 个块,将输出呈现为 (1000/20=)50 行、6 列 df。

我已经尝试为我的初始 df 创建一个索引列,它在前 20 行重复 1,在接下来的 20 行重复 2,依此类推。

    convert_n <- function(df) {
    df <- df %T>% {.$n_for_t_tests = rep(c(1:(nrow(df)/20)), each = 20)}
    }
    df <- convert_n(df)

但是,我似乎找不到正确利用此列中的项目作为“for”或任何类型循环的索引的方法。

您可以在下面看到创建 1 行 6 列 df 的相关代码;我需要修改 [0:20] 部分,创建一个循环,为 20 个组执行此操作并绑定它们。

    t_test_a <- t.test(df$a1[0:20], dfff$a2[0:20], paired = T, conf.level 
    = 0.95)
    t_test_b <- t.test(df$b1[0:20], dfff$b2[0:20], paired = T, conf.level 
    = 0.95)
    t_test_c <- t.test(df$c1[0:20], dfff$c2[0:20], paired = T, conf.level 
    = 0.95)
    t_tests_df <- data.frame(t_a = t_test_a$statistic[["t"]], 
                             t_b = t_test_b$statistic[["t"]],
                             t_c = t_test_c$statistic[["t"]])

    t_tests_df <- t_tests_df %T>% {.$dif_significance_a = ifelse(.$t_a > 
                                   2, "YES", "NO")} %T>% 
                                  {.$dif_significance_b = ifelse(.$t_b > 
                                   2, "YES", "NO")} %T>% 
                                  {.$dif_significance_c = ifelse(.$t_c > 
                                   2, "YES", "NO")} %>% 
                                  dplyr::select(t_a, dif_significance_a, 
                                                t_b, dif_significance_b,
                                                t_c, dif_significance_c)

提前感谢您的帮助。

【问题讨论】:

  • 索引从1开始,所以df$a1[1:20]等等
  • 其实这也行。

标签: r loops dataframe t-test


【解决方案1】:

您可以使用split()sapply()

set.seed(42)

df <- data.frame(a1 = sample(1000, 1000), a2 = sample(1000, 1000),
                 b1 = sample(1000, 1000), b2 = sample(1000, 1000),
                 c1 = sample(1000, 1000), c2 = sample(1000, 1000))

group <- gl(50, 20)

D <- split(df, group)

myt <- function(Di) 
  with(Di, c(at=t.test(a1, a2)$statistic, ap=t.test(a1, a2)$p.value,
    bt=t.test(b1, b2)$statistic, bp=t.test(b1, b2)$p.value,
    ct=t.test(c1, c2)$statistic, cp=t.test(c1, c2)$p.value))

sapply(D, FUN=myt) ### or
t(sapply(D, FUN=myt))

【讨论】:

    【解决方案2】:

    这不是最漂亮的,但我做了一个这样的 for 循环:

    df <- data.frame(a1 = sample(1000, 1000),
                     a2 = sample(1000, 1000),
                     b1 = sample(1000, 1000),
                     b2 = sample(1000, 1000),
                     c1 = sample(1000, 1000),
                     c2 = sample(1000, 1000))
    
    
    df_ttest <- data.frame(p_a = c(1:50),
                           t_a = c(1:50),
                           p_b = c(1:50),
                           t_b = c(1:50),
                           p_c = c(1:50),
                           t_c = c(1:50))
    
    index <- 0:50*20
    
    for(i in seq_along(index)) {
        df_ttest$p_a[i] =  t.test(df$a1[index[i] : index[i+1]])$p.value
        df_ttest$p_b[i] =  t.test(df$b1[index[i] : index[i+1]])$p.value
        df_ttest$p_c[i] =  t.test(df$c1[index[i] : index[i+1]])$p.value
    
        df_ttest$t_a[i] =  t.test(df$a1[index[i] : index[i+1]])$statistic
        df_ttest$t_b[i] =  t.test(df$b1[index[i] : index[i+1]])$statistic
        df_ttest$t_c[i] =  t.test(df$c1[index[i] : index[i+1]])$statistic
    }
    

    这为 a、b 和 c 的每 20 行块提供了一个 50x6 的数据框,其中包含 p 和 t 值的单独列。

    您甚至可以更进一步,创建一个嵌套的 for 循环来循环遍历 df_ttest 中的每一行,以使这更漂亮。

    【讨论】:

    • 第二部分正是我想要的,我相信只要稍加修改,它就会对我有用,谢谢!
    • 我实际上无法让它工作,因为小修改结果“不是那么小”......我看到了索引使用中的逻辑,但我的 t 测试要配对,什么时候我在 t.test 中引入了另一个参数,例如“df$a2[index[i] : index[i+1]”,我遇到了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2018-01-18
    • 2016-04-22
    相关资源
    最近更新 更多