R,从t.test中提取每一行的p值

时间:2018-08-19 17:45:01

标签: r

我正在尝试对每一行进行t.test,然后提取p值进行绘图。作为参考,我发现了这则旧文章:output p value from a t-test in R

这是我的摘录:

PATH=/sbin:/bin:/usr/bin

但是当我尝试调用它时,我只返回“ NULL”吗?下面是我的几行数据,仅供参考。

> pVal143<-apply(mRNA143.data, 1, t.test)$p.value

使用原始数据集“ c007”进行更新(我需要比较“ C”值和H值中的p值)。

       c.mRNA    h.mRNA
1    8.224342  8.520142
2    9.096665 11.762597
3   10.698863 10.815275
4   10.666233 10.972130
5   12.043525 12.140297

1 个答案:

答案 0 :(得分:0)

一种解决方案,您必须手动指定哪些列属于每个组进行比较:

# example data
df = read.table(text = "
C1       C2      C3     C4       C5     C6     H1    H2 H3  H4  H5  H6
8.57345 8.45938 8.68941 8.35913 8.48177 8.44560 8.40986 8.59392 8.46562 8.07999 8.22759 8.41817
8.32595 8.19273 8.10708 8.48156 7.99014 8.24859 8.78216 8.59592 8.48299 8.52647 8.34797 8.38534
", header=T)

library(tidyverse)

df %>%
  rowwise() %>%
  mutate(pval = t.test(c(C1,C2,C3,C4,C5,C6),
                       c(H1,H2,H3,H4,H5,H6))$p.value) %>%
  ungroup()

# # A tibble: 2 x 13
#      C1    C2    C3    C4    C5    C6    H1    H2    H3    H4    H5    H6   pval
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
# 1  8.57  8.46  8.69  8.36  8.48  8.45  8.41  8.59  8.47  8.08  8.23  8.42 0.161 
# 2  8.33  8.19  8.11  8.48  7.99  8.25  8.78  8.60  8.48  8.53  8.35  8.39 0.0110

一种替代解决方案,您可以重塑数据并从每列的第一个字母创建两个组:

df %>%
  mutate(id = row_number()) %>%                 # add row id
  gather(key, value, -id) %>%                   # reshape dataset
  mutate(key = substr(key,1,1)) %>%             # create a group column from first letter (will be used for the t.test comparison)
  group_by(id) %>%                              # for each row
  summarise(pval = t.test(value ~ key)$p.value) # get p value 

# # A tibble: 2 x 2
#      id   pval
#   <int>  <dbl>
# 1     1 0.161 
# 2     2 0.0110