【问题标题】:How do I perform a t test in R comparing values in the same row of a data frame?如何在 R 中执行 t 检验比较数据帧同一行中的值?
【发布时间】:2021-05-06 11:15:00
【问题描述】:

我有以下数据框:

  Gene    WT1    WT2     WT3    KO1    KO2   KO3
  <chr> <dbl>  <dbl>   <dbl>  <dbl>  <dbl> <dbl>
1 BIG2  -4.46 -5.25  -5.01   -4.59  -3.47  -5.16
2 CAVN1 -4.71 -4.78  -4.69   -4.53  -4.62  -5.14
3 HVM03 -5.31 -5.63  -3.98   -0.418 -0.194 -4.21
4 DYN1  -2.09 -0.292 -0.0488 -5.13  -5.90  -4.96
5 ACSA   4.62  4.42   4.62   -5.32  -3.83  -4.08

我想对每一行进行 t.test,将 3 个 WT 值与 3 个 KO 值进行比较,并在数据框末尾添加一个 p 值的新列。如果你能帮忙,请告诉我

【问题讨论】:

    标签: r dataframe dplyr tibble


    【解决方案1】:

    您可以逐行应用t.test 并提取p.value。

    library(dplyr)
    
    df %>%
      rowwise() %>%
      mutate(p.value = t.test(c_across(starts_with('WT')), 
                              c_across(starts_with('KO')))$p.value) %>%
      ungroup
    
    #  Gene    WT1    WT2     WT3    KO1    KO2   KO3 p.value
    #  <chr> <dbl>  <dbl>   <dbl>  <dbl>  <dbl> <dbl>   <dbl>
    #1 BIG2  -4.46 -5.25  -5.01   -4.59  -3.47  -5.16 0.433  
    #2 CAVN1 -4.71 -4.78  -4.69   -4.53  -4.62  -5.14 0.866  
    #3 HVM03 -5.31 -5.63  -3.98   -0.418 -0.194 -4.21 0.109  
    #4 DYN1  -2.09 -0.292 -0.0488 -5.13  -5.9   -4.96 0.00971
    #5 ACSA   4.62  4.42   4.62   -5.32  -3.83  -4.08 0.00222
    

    【讨论】:

    • 谢谢,我收到一条错误消息,说“c_across() 只能在 dplyr 动词中使用”知道为什么它不起作用吗? @Ronak Shah
    • @David_G 是的,你可能已经加载了plyr,它掩盖了mutate 函数。尝试使用 dplyr::mutate 或重新启动 R 并仅加载 dplyr
    【解决方案2】:

    使用startsWith + mapply 的基本 R 选项

    WT <- data.frame(t(df[startsWith(names(df), "WT")]))
    KO <- data.frame(t(df[startsWith(names(df), "KO")]))
    df$p.value <- mapply(function(x, y) t.test(x, y)$p.value, WT, KO)
    

    给予

    > df
       Gene   WT1    WT2     WT3    KO1    KO2   KO3     p.value
    1  BIG2 -4.46 -5.250 -5.0100 -4.590 -3.470 -5.16 0.432649677
    2 CAVN1 -4.71 -4.780 -4.6900 -4.530 -4.620 -5.14 0.865600809
    3 HVM03 -5.31 -5.630 -3.9800 -0.418 -0.194 -4.21 0.108804979
    4  DYN1 -2.09 -0.292 -0.0488 -5.130 -5.900 -4.96 0.009712383
    5  ACSA  4.62  4.420  4.6200 -5.320 -3.830 -4.08 0.002216407
    

    数据

    > dput(df)
    structure(list(Gene = c("BIG2", "CAVN1", "HVM03", "DYN1", "ACSA"
    ), WT1 = c(-4.46, -4.71, -5.31, -2.09, 4.62), WT2 = c(-5.25,
    -4.78, -5.63, -0.292, 4.42), WT3 = c(-5.01, -4.69, -3.98, -0.0488,
    4.62), KO1 = c(-4.59, -4.53, -0.418, -5.13, -5.32), KO2 = c(-3.47,
    -4.62, -0.194, -5.9, -3.83), KO3 = c(-5.16, -5.14, -4.21, -4.96,
    -4.08)), class = "data.frame", row.names = c("1", "2", "3", "4",
    "5"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2021-12-24
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多