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!
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
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With