独特字符的频率

时间:2015-04-23 22:12:10

标签: r

我试图找到在具有各自pvalue的样本中发现独特基因的次数

df1 <-  read.table(text="
        Gene        id           Seg.mean    pValue    CNA
         Nfib       8410          0.3108     1.381913 gain
         Mycl       8410          2.7320     1.182842 gain
         Mycl       8410          2.7320     1.846275 gain
         Nfib       8411          0.5920     1.381913 gain
         Nfib       8411          1.3090     1.381913 gain
         Mycl       8412          1.6150     5.765442 gain
         Mycl       8411          1.6150     1.846275 gain
",header=TRUE)

预期产出

Gene    ID           Freq. of id   pValue
Nfib    8410,8411        2           1.381913
Mycl    8410,8411,8412   3           1.182842,1.846275,5.765442

3 个答案:

答案 0 :(得分:2)

溶胶&#39; N:

library(dplyr)

df1 %>% 
  group_by(Gene) %>% 
  summarise(ID = paste0(unique(id), collapse=", "),
            pval = paste0(unique(pValue),collapse=", "), 
            n = n_distinct(id))

结果:

  Gene               ID                         pval n
1 Mycl 8410, 8412, 8411 1.182842, 1.846275, 5.765442 3
2 Nfib       8410, 8411                     1.381913 2

击穿:

  1. 我们要评估Gene(分析单位),group_by(Gene)
  2. 然后创建对应于paste0(var,collapse=", ")的新变量。这是应用 per Gene
  3. 计算不同ID的数量。再次应用 per Gene

答案 1 :(得分:1)

我认为您可以使用data.table来非常接近您想要实现的结果:

library(data.table)

df1<-data.table(df1)
df1[,
list(ID = paste(unique(id), collapse=','),
     "Freq. of id"=length(unique(id)), 
     pValue=paste(unique(pValue), collapse=",")),
keyby=list(Gene)]

答案 2 :(得分:1)

library(plyr)
> ddply(data.frame(df1), .(Gene), summarise,ID=paste(unique(id), collapse=","),pValue=paste(unique(pValue), collapse=","),Freq = length(unique(id)))
  Gene             ID                     pValue Freq
1 Mycl 8410,8412,8411 1.182842,1.846275,5.765442    3
2 Nfib      8410,8411                   1.381913    2
相关问题