基于strsplit将数据帧重组为多行
我有这样的数据结构基于strsplit将数据帧重组为多行,r,strsplit,R,Strsplit,我有这样的数据结构 structure(list(id = c("4031", "1040;2040;3040", "4040", "1050;2050;3050"), description = c("Sentence A", "Sentence B", "Sentence C", "Sentence D")), row.names = 1:4, class = "data.frame") id description 1
structure(list(id = c("4031", "1040;2040;3040", "4040",
"1050;2050;3050"), description = c("Sentence A",
"Sentence B", "Sentence C",
"Sentence D")), row.names = 1:4, class = "data.frame")
id description
1 4031 Sentence A
2 1040;2040;3040 Sentence B
3 4040 Sentence C
4 1050;2050;3050 Sentence D
我希望重新构造数据,以便将带有“;”的ID拆分为单独的行-我希望这样:
structure(list(id = c("4031", "1040","2040","3040", "4040",
"1050","2050","3050"), description = c("Sentence A",
"Sentence B","Sentence B","Sentence B", "Sentence C",
"Sentence D","Sentence D","Sentence D")), row.names = 1:8, class = "data.frame")
id description
1 4031 Sentence A
2 1040 Sentence B
3 2040 Sentence B
4 3040 Sentence B
5 4040 Sentence C
6 1050 Sentence D
7 2050 Sentence D
8 3050 Sentence D
我知道我可以使用strsplit
拆分id列,但无法找到一种有效的方法,在没有循环的情况下将其转换为行
strsplit( as.character( a$id ) , ";" )
使用
tidyr
的一个非常方便的可能性是:
separate_rows(df, id)
id description
1 4031 Sentence A
2 1040 Sentence B
3 2040 Sentence B
4 3040 Sentence B
5 4040 Sentence C
6 1050 Sentence D
7 2050 Sentence D
8 3050 Sentence D
使用R基:
> IDs <- strsplit(df$id, ";")
> data.frame(ID=unlist(IDs), Description=rep(df$description, lengths(IDs)))
ID Description
1 4031 Sentence A
2 1040 Sentence B
3 2040 Sentence B
4 3040 Sentence B
5 4040 Sentence C
6 1050 Sentence D
7 2050 Sentence D
8 3050 Sentence D
IDs data.frame(ID=unlist(IDs),Description=rep(df$Description,length(IDs)))
ID描述
14031 A句
21040 B句
3 2040 B句
43040 B句
54040 C句
61050 D句
7 2050 D句
83050句D