如何在R中转换数据

时间:2015-08-10 07:12:43

标签: r

我有这种数据......

Id             Name                   gid    
GO:0019992  diacylglycerol binding  23025    
GO:0019992  diacylglycerol binding  10497    
GO:0045703  ketoreductase activity  8644    
GO:0016519  gastric inhibitory peptide receptor activity    2696    
GO:0035174  histone serine kinase activity  5562    
GO:0035174  histone serine kinase activity  5563    
GO:0035174  histone serine kinase activity  6795    
GO:0030298  receptor signaling protein tyrosine kinase activator activity   6352   
GO:0030292  protein tyrosine kinase inhibitor activity  11116   
GO:0030292  protein tyrosine kinase inhibitor activity  10399

我必须将其更改为

GO:0019992  diacylglycerol binding  23025 10497    
GO:0045703  ketoreductase activity  8644    
GO:0016519  gastric inhibitory peptide receptor activity    2696    
GO:0035174  histone serine kinase activity  5562 5563 472 6790 9212 6795    
GO:0035175  histone kinase activity (H3-S10 specific)   7443    
GO:0030298  receptor signaling protein tyrosine kinase activator activity   6352    
GO:0030292  protein tyrosine kinase inhibitor activity  11116 10399

我怎样才能在R?

中完成

3 个答案:

答案 0 :(得分:3)

你可以使用data.table:

到达那里
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input name="name1" type="text" />
  <button class="button">+</button>
</div>

答案 1 :(得分:1)

假设您的data.frame名为df

基地:

new_var <- unlist(
  lapply(
    split(df,f = df$Name), 
    function(x) paste0(x$gid, collapse= " ")
  )
)
df <- df[unique(df[,1:2]),]
df$new_var <- new_var

答案 2 :(得分:0)

除了其他答案之外,还有使用dplyr套餐的另一种方法:

library(dplyr)
df = df %>% 
     group_by(Id, Name) %>% 
     summarise(gids = paste(gid, collapse = " "))