从另一个表的多个列创建一个表

时间:2018-06-11 09:17:52

标签: r

我有一个包含三列的表ABC,

Hugo_Symbol   Consequence        Tumor_Sample
AACS        missense_variant    WCMC10362_2_C
AADACL4     missense_variant    WCMC188_1_C
AADACL4     missense_variant    WCMC189_1_C
AADACL4     missense_variant    WCMC10362_2_C
AASS         splice_variant     WCMC10362_2_C
ABCA13      missense_variant    WCMC188_1_C
ABCA13      missense_variant    WCMC10362_2_C

我需要使用ABC的三列

中的值创建一个新表df1
Hugo_Symbol  WCMC188_1_C       WCMC189_1_C      WCMC10362_2_C
AACS         NA                NA               missense_variant
AADACL4    missense_variant   missense_variant  missense_variant        
AASS         NA                NA               splice_variant
ABCA13     missense_variant    NA               missense_variant

我没有找到允许我这样做的代码。如果有代码在表中输入值,我可以按如下方式创建第二个表的结构。

Hugo_Symbol  WCMC188_1_C       WCMC189_1_C      WCMC10362_2_C
AACS        
AADACL4     
AASS        
ABCA13

2 个答案:

答案 0 :(得分:0)

这里有一个简单的传播。

df1 <- df %>%
  spread( key = Tumor_Sample, value = Consequence, fill = NA)  

答案 1 :(得分:0)

#You can try with reshape package and the function recast.
library(reshape2)
recast(df1, df1$Hugo_Symbol + variable ~ df1$Tumor_Sample, id.var = 
c("Hugo_Symbol", "Tumor_Sample"))