R data.table根据字符串值将一行拆分为多行



我有一个这样的数据表:

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

我怎么能得到这个:

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

提前感谢!

下面是另一个使用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

以下是一种适用于tstrsplit的方法:

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

数据

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"))

这是我的dplyr解决方案:

dt %>% 
group_by(title) %>% 
separate_rows(sha, sep = ";") %>% 
mutate(sha = as.numeric(sha)) %>% 
filter(!is.na(sha))

应该给你这个:

# A tibble: 8 x 2
# Groups:   title [4]
title              sha
<chr>            <dbl>
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

最新更新