【问题标题】:Pearson's correlation coefficient with from two tables in r皮尔逊相关系数与 r 中的两个表
【发布时间】:2018-05-09 22:29:17
【问题描述】:

我有以下两个数据集:

df <- read.table(text =
                   "Human_Gene_Name hsapiens    mmusculus   ggallus celegans    dmelanogaster   cintestinalis   trubripes   xtropicalis mmulatta
A1CF    5.634789603 4.787491743 3.688879454 2.079441542 3.931825633 2.772588722 3.871201011 3.044522438 4.094344562
                 AAK1   3.583518938 2.708050201 2.079441542 2.197224577 2.079441542 0.693147181 2.772588722 2.079441542 3.218875825
                 AAMP   3.555348061 3.17805383  2.48490665  1.791759469 2.302585093 0.693147181 2.48490665  1.098612289 2.079441542", header  = T)

ctn_df <- read.table(text = "Species    CTN
                     hsapiens   158
                     mmusculus  85
                     ggallus    67
                     celegans   32
                     dmelanogaster  27
                     cintestinalis  19
                     trubripes  110
                     xtropicalis    82
                     mmulatta   71
                     ", header = T)

“df”中的值代表功能多样性,我想根据物种 CTN 和功能多样性计算每个基因的 pearsons 相关系数。

有没有一种方法可以根据“ctn_df”中的数据轻松地将 CTN 分配给“df”表中的特定物种。

对不起,如果这是一个简单的问题。

【问题讨论】:

    标签: r dataframe statistics bioinformatics biomart


    【解决方案1】:

    使用apply 将行数值作为第一个参数连续传递给cor,然后用第一列命名相关值:

    setNames( apply(df[-1], 1, cor, ctn_df$CTN), df$Human_Gene_Name)
         A1CF      AAK1      AAMP 
    0.7556590 0.7834861 0.6829534 
    

    【讨论】:

      【解决方案2】:

      这是Tidyverse 解决方案:

      library(tidyverse)
      
      gather(df, Species, functional_diveresity, -Human_Gene_Name) %>%
        left_join(ctn_df) %>%
        group_by(Human_Gene_Name) %>%
        summarise(cor(functional_diveresity, CTN))
      
      #  # A tibble: 3 x 2
      #   Human_Gene_Name `cor(functional_diveresity, CTN)`
      #   <fct>                                       <dbl>
      # 1 A1CF                                        0.756
      # 2 AAK1                                        0.783
      # 3 AAMP                                        0.683
      

      前两行产生一个tidy dataframe,这使得下游计算更容易:

      gather(df, Species, functional_diveresity, -Human_Gene_Name) %>%
        left_join(ctn_df)
      
      #    Human_Gene_Name       Species functional_diveresity CTN
      # 1             A1CF      hsapiens             5.6347896 158
      # 2             AAK1      hsapiens             3.5835189 158
      # 3             AAMP      hsapiens             3.5553481 158
      # 4             A1CF     mmusculus             4.7874917  85
      # 5             AAK1     mmusculus             2.7080502  85
      # 6             AAMP     mmusculus             3.1780538  85
      # ....
      

      【讨论】:

        猜你喜欢
        • 2021-08-29
        • 1970-01-01
        • 2016-03-28
        • 2011-09-10
        • 2016-08-12
        • 1970-01-01
        • 2011-09-13
        • 2012-11-19
        • 2013-10-12
        相关资源
        最近更新 更多