2016-07-07 100 views
-1

任何人都可以請幫助這個小data.frame擴展問題? 在此先感謝!R數據幀,通過字符串變量擴展行

# I have 
data.frame(rbind(c("1", "2", "3", "a/b/c"), 
       c("11", "0", "5", "c/d"), 
       c("3", "33", "0", "a")) 
      ) 

# X1 X2 X3 X4 
# 1 1 2 3 a/b/c 
# 2 11 0 5 c/d 
# 3 3 33 0  a 

# I want 
data.frame(rbind(c("1", "2", "3", "a"), 
       c("1", "2", "3", "b"), 
       c("1", "2", "3", "c"), 
       c("11", "0", "5", "c"), 
       c("11", "0", "5", "d"), 
       c("3", "33", "0", "a")) 
      ) 

# X1 X2 X3 X4 
# 1 1 2 3 a 
# 2 1 2 3 b 
# 3 1 2 3 c 
# 4 11 0 5 c 
# 5 11 0 5 d 
# 6 3 33 0 a 
+3

見splitstackshape包。 – zx8754

回答

1

我們可以使用data.table

library(data.table) 
setDT(df1)[, strsplit(as.character(X4), "/"), by = .(X1, X2, X3)] 
+0

@ zx8754已經完成。它 – akrun