Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R data.table split a row into multiple rows based on string values [duplicate]

Tags:

r

data.table

I have a data.table like this:

dt <- data.table("title"=c("First Title", "Second Title", "Third Title", "Fourth Title"), 
                 "sha"=c("12345", "2345; 66543; 33423", "22222; 12345678;", "666662345; 444"))

How can I get this:

      title                sha
1:  First Title           12345
2: Second Title           2345 
3: Second Title           66543
4: Second Title           33423
5:  Third Title           22222
6:  Third Title           12345678
7: Fourth Title           666662345
8: Fourth Title           444

Thanks in advance!

like image 949
Blind0ne Avatar asked Oct 20 '25 02:10

Blind0ne


2 Answers

Here is another solution using data.table:

dt[, .(sha = unlist(tstrsplit(sha, ";", type.convert = TRUE))), by = "title"]

#           title       sha
# 1:  First Title     12345
# 2: Second Title      2345
# 3: Second Title     66543
# 4: Second Title     33423
# 5:  Third Title     22222
# 6:  Third Title  12345678
# 7: Fourth Title 666662345
# 8: Fourth Title       444
like image 104
B. Christian Kamgang Avatar answered Oct 21 '25 16:10

B. Christian Kamgang


Here's one approach with tstrsplit that should work for you:

library(data.table)
dt[, lapply(.SD, function(x) unlist(tstrsplit(x, "; ?"))),
   .SDcols = "sha",by = c("title","date")]
          title     date       sha
1:  First Title 1/1/2020     12345
2: Second Title 1/2/2020      2345
3: Second Title 1/2/2020     66543
4: Second Title 1/2/2020     33423
5:  Third Title 1/3/2020     22222
6:  Third Title 1/3/2020  12345678
7: Fourth Title 1/4/2020 666662345
8: Fourth Title 1/4/2020       444

Data

dt <- data.table("title"=c("First Title", "Second Title", "Third Title", "Fourth Title"), 
                  "sha"=c("12345", "2345; 66543; 33423", "22222; 12345678;", "666662345; 444"),
                  "date" = c("1/1/2020","1/2/2020","1/3/2020","1/4/2020"))
like image 43
Ian Campbell Avatar answered Oct 21 '25 16:10

Ian Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!