R - 我想以 ";" 作为分隔符拆分/分区字符串单元格并从中创建行



我试图弄清楚如何做到这一点,但不确定如何做到这一点。 例如,如果我有...

ID  Name  Type                 Other
1   Andy  Test1; Test2; Test3  Other1; Other2; Other3

我想让 R 回来...

ID  Name  Type   Other
1   Andy  Test1  Other1
1   Andy  Test2  Other2
1   Andy  Test3  Other3

有没有办法对数据进行分区来获得它?

str1="Test1; Test2; Test3"
str2="Other1; Other2; Other3"
Name="Andy"
Type=unlist(strsplit(str1,";"))
Other=unlist(strsplit(str2,";"))
cbind(Name,Type,Other)
> cbind(Name,Type,Other)
     Name   Type     Other    
[1,] "Andy" "Test1"  "Other1" 
[2,] "Andy" " Test2" " Other2"
[3,] "Andy" " Test3" " Other3"

最新更新