【问题标题】:Using R to stack non-numerical dataset使用 R 堆叠非数值数据集
【发布时间】:2018-08-22 20:13:16
【问题描述】:

我有一个需要堆叠的 GO 术语的 data.frame。 df 看起来像:

> head(GO_info)
      V2 
Gene1 GO:0003674,GO:0005215,GO:0005216,GO:0005575
Gene2 GO:0000462,GO:0002181,GO:0003674,GO:0003735

但我希望这个数据框堆叠起来,包括标题“ind”和“values”,例如:

ind values
Gene1 GO:0003674
Gene1 GO:0005215
etc

我尝试使用:

GO_info2 <- stack(GO_info)

但这不起作用,R帮助功能对我没有多大帮助。有人可以帮帮我吗?

【问题讨论】:

  • 我看到我的 >head(GO_info) 是一个小错误。第 1 列有一个名为 V1 的标题,所以它看起来像: V1 V2 Gene1 GO:0003674,GO:0005215,GO:0005216,GO:0005575 Gene2 GO:0000462,GO:0002181,GO:0003674,GO:0003735 因为代码A. Suliman 不工作 我现在尝试如下:GOterm &lt;- data.frame(V2=unlist(strsplit(as.character(GOterm1$V2),","))) 但是 V1 被完全删除,而这对于标识符很重要。有什么帮助吗?
  • 如果您有两列 V1 和 V2,那么您可以使用 df %&gt;% separate_rows(V2,sep = ',')。然后使用 colnames(df) &lt;- c('ind','values') 更改列名

标签: r stack


【解决方案1】:
 library(tidyr)
 df %>% separate(V2,into = c('ind','values'),sep = '\\s') %>%  #separate V2 to inds and values at space \\s
        separate_rows(values,sep = ',')                        #separate values to multiple rows at ','

      ind     values
  1 Gene1 GO:0003674
  2 Gene1 GO:0005215
  3 Gene1 GO:0005216
  4 Gene1 GO:0005575
  5 Gene2 GO:0000462
  6 Gene2 GO:0002181
  7 Gene2 GO:0003674
  8 Gene2 GO:0003735

数据

 df<-read.table(text="
           V2 
           'Gene1 GO:0003674,GO:0005215,GO:0005216,GO:0005575'
           'Gene2 GO:0000462,GO:0002181,GO:0003674,GO:0003735'
           ",header=TRUE)

【讨论】:

  • 感谢您的评论,但它似乎还没有工作。我尝试实现它:GOterm &lt;- GO_info %&gt;% separate(V2,into = c('ind','values'),sep = '\\s') %&gt;% GOterm=new stacked datafile and GO_info my old df with 1000s of gene ID's. I get the following warning: Warning message: Expected 2 pieces. Missing pieces filled with NA` 在 24140 行 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...]。它只是将我的基因名称(如“TRINITY_DN100009_c0_g1”)替换为值 1、2、3、4。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2021-05-28
  • 1970-01-01
相关资源
最近更新 更多