Home > Net >  Expand rows with multiple information into unique rows with a single information
Expand rows with multiple information into unique rows with a single information

Time:09-29

I have a dataset in R which looks like that:

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

I would like to expand the rows in order to get a dataset that looks like that:

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

Any help would be very appreciated!

Thanks a lot in advance.

CodePudding user response:

Here's a way with separate_rows and 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  
  • Related