1

假设我有以下数据框

> df <- data.frame(var1 = c("A", "B", "C", "D"),            
                 var2 = c("test", "5 | 6", "X & Y", "M | N | O"))
> df
  var1          var2
1    A          test
2    B         5 | 6
3    C         X & Y
4    D     M | N | O

如何var2通过|and&运算符拆分值并将它们作为单独的行放入same data.frame. 输出应如下所示:

> df
  var1          var2
1    A          test
2    B             5
3    B             6
4    C             X
5    C             Y
6    D             M
7    D             N
8    D             O

我使用strsplit了一个for循环来实现它。但是,我认为这不是很好的编码。任何想法如何以更好的 R 方式实现这一目标?

4

2 回答 2

6

你可以这样做:

s <- strsplit(df[,2], " \\| | & ")
cbind(var1=rep(df[,1], sapply(s, length)), var2=unlist(s))
     var1 var2  
[1,] "A"  "test"
[2,] "B"  "5"  
[3,] "B"  "6"  
[4,] "C"  "X"  
[5,] "C"  "Y"  
[6,] "D"  "M"  
[7,] "D"  "N" 
[8,] "D"  "O"  
于 2012-07-17T07:50:43.110 回答
1

另一种选择是cSplit从我的“splitstackshape”包中使用:

library(splitstackshape)
cSplit(df, "var2", "[|&]", "long", fixed = FALSE)[var2_new != ""]
#    var1 var2_new
# 1:    A     test
# 2:    B        5
# 3:    B        6
# 4:    C        X
# 5:    C        Y
# 6:    D        M
# 7:    D        N
# 8:    D        O
于 2014-12-23T13:04:34.440 回答