【问题标题】:Expand rows with multiple information into unique rows with a single information将具有多个信息的行扩展为具有单个信息的唯一行
【发布时间】:2022-09-28 22:42:10
【问题描述】:

我在 R 中有一个数据集,如下所示:

Id Pb_cit
1 2000(19)|5600(24)
2 3000(79)|87600(198)

我想扩展行以获得如下所示的数据集:

Id Pb_id Cites
1 2000 19
1 5600 24
2 3000 79
2 87600 198

任何帮助将不胜感激!

提前非常感谢。

    标签: r duplicates expand


    【解决方案1】:

    这是separate_rows 和extract 的一种方式:

    library(tidyr)
    dat %>% 
      separate_rows(Pb_cit, sep = "\\|") %>% 
      extract(Pb_cit, into = c("Pb_id", "Cites"), "(\\d+)\\((\\d+)\\)")
    
    #     Id Pb_id Cites
    #1     1 2000  19   
    #2     1 5600  24   
    #3     2 3000  79   
    #4     2 87600 198  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2020-09-11
      相关资源
      最近更新 更多