合并具有多个值的列

时间:2015-04-15 12:16:24

标签: r merge apply bioinformatics

我有一个数据框cluster,其中一列cluster$Genes如下所示:

ENSG00000134684
ENSG00000188846, ENSG00000181163, ENSG00000114391
ENSG00000134684, ENSG00000175390
ENSG00000134684
ENSG00000134684, ENSG00000175390
...

列中每行中的元素数是任意的。我还有另一个数据框expression,如下所示:

ENSGID           a       b
ENSG00000134684  1       3
ENSG00000175390  2       0
ENSG00000000419  131.23  108.73
ENSG00000000457  7.11    8.68
ENSG00000000460  15.70   6.59
ENSG00000000938  0       0
ENSG00000000971  0.03    0.07
ENSG00000001036  59.22   58.3
...

...并且有大约20000行。我想做的是:

  1. 对于cluster$Genes中每行中的所有元素,找到相应的ab
  2. a
  3. 中的每一行计算bcluster$Genes(单独)的最小值,最大值和平均值
  4. cluster数据框中创建六个新列,并使用(min.a, max.a, mean.a, min.b, max.b, mean.b)值填充
  5. 我试图找到一些方法来做到这一点,但情况并不顺利。在谷歌搜索帮助时,我想我可能会使用某种apply,我得到了一些代码。我认为这主要是胡言乱语,完全没有功能,而且我有点卡住了。这就是我得到的:

    exp.lookup = function(genes) {
      genes.split = strsplit(genes, ', ')
      exp.hct = list()
      exp.hke = list()
      for ( gene in genes.split ) {
        exp.hct = c(exp.hct, merge(gene, means$hct, all.x=TRUE))
        exp.hke = c(exp.hke, merge(gene, means$hke, all.x=TRUE))
        return(c(exp.hct, exp.hke))
      }
    }
    
    apply(cluster['Genes'], 1, FUN=exp.lookup)
    

    任何人都有更好的想法,这可能确实有用吗?

2 个答案:

答案 0 :(得分:4)

重新创建初始数据:

library(data.table)

cluster<- as.data.table(list(Genes = c("ENSG00000134684",
                                       "ENSG00000188846, ENSG00000181163, ENSG00000114391", 
                                       "ENSG00000134684, ENSG00000175390", 
                                       "ENSG00000134684", 
                                       "ENSG00000134684, ENSG00000175390")))

expression<- as.data.table(list(ENSGID = c("ENSG00000134684", "ENSG00000175390",
                                           "ENSG00000000419", "ENSG00000000457",
                                           "ENSG00000000460", "ENSG00000000938",
                                           "ENSG00000000971", "ENSG00000001036"),
                                a = c(1,2,131.23,7.11,15.70, 0, 0.03, 59.22),
                                b = c(3,0,108.73,8.68,6.59,0,0.07,58.3)))
setkey(cluster, Genes)
setkey(expression, ENSGID)

解决方案:

library(data.table)

result<- function() {
  colnames<- c("min.a", "max.a", "mean.a", "min.b", "max.b", "mean.b")
  # 1. "(colnames)" is parenthesized to insure we are adding new columns from
  # colnames variable by reference and evaluates to character vector with 
  # new columns names
  # 2. ":=" is for adding new columns to existing data.table by reference
  # 3. "count(Genes)" calls count() function over "Genes" column, but as long
  # as we are using grouping "by = Genes", count() works with each row turn
  # by turn. And each row is a character vector.
  cluster[,(colnames):=count(Genes), by = Genes]
}

# get Genes row
count<- function(charvector) {
  ENSGIDc<- strsplit(charvector, ", ")
  # 4. subsetting "expression" data.table rows by splitted "Genes" character 
  # vector named "ENSGIDc"...
  # 5. ... and then calculating column's maxes, mins and means
  expression[ENSGIDc, .(min(a, na.rm = T), max(a, na.rm = T),
                        mean(a, na.rm = T), min(b, na.rm = T), 
                        max(b, na.rm = T), mean(b, na.rm = T))]
  # 6. at this point we are returning resulting 1 row 6 columns data.table     
  # back to calling function, where it's added to "cluster" data.table
}

suppressWarnings(result())

答案 1 :(得分:0)

假设每个ENSGID对应一对唯一的a和b值,我建议:

  1. cluster$Genes分配给变量(换句话说,将其复制到cluster数据框之外)。例如,new_cluster_genes <- cluster$Genes

  2. 操纵new_cluster_genes,以便每行都有一个ENSGID。添加名为ENSGID的列标题。

  3. 使用new_cluster_genes作为公共ID,将ENSGID与表达式数据框合并。将结果数据框分配给变量:例如,merged_genes

  4. 计算每行的a和b(单独)的最小值,最大值和平均值:

    library(dplyr) 
    merged_genes %>% 
    mutate(min.a = min(a),
           max.a = max(a), 
           mean.a = mean(a), 
           min.b = min(b), 
           max.b = max(b), 
           mean.b = mean(b)) -> merged_genes
    
  5. 创建6个新列并使用(min.a,max.a,mean.a,min.b,max.b,mean.b)值填充它们:

    merged_genes %>% select(ENSGID, min.a:mean.b) -> merged_genes_subset
    
  6. 操纵cluster数据框,以便每行都有一个ENSGID。添加名为ENSGID的列标题。使用merged_genes_subset作为公共ID,将ENSGID与群集合并。