如何按名称拆分数据框的列?

时间:2017-12-25 13:40:29

标签: r dataframe split

我有一个数据帧df:

a.1 b.2  c.33 ...
1   kuku 3
44  bubu 441
55  wewe 4441

我想将列分割为a 1 b 2 c 33,如下所示:

a   1  b    2
1   1  kuku kuku 
44  44 bubu bubu 
55  55 wewe wewe .....

如何做到这一点,请指教?

1 个答案:

答案 0 :(得分:2)

从:

开始
mydf <- structure(list(a.1 = c(1L, 44L, 55L), b.2 = c("kuku", "bubu", 
    "wewe"), c.33 = c(3L, 441L, 4441L)), .Names = c("a.1", "b.2", 
    "c.33"), row.names = c(NA, 3L), class = "data.frame")

使用repstrsplit(名称)的组合:

setNames(data.frame(rep(mydf, each = 2)), 
         unlist(strsplit(names(mydf), ".", TRUE)))
##    a  1    b    2    c   33
## 1  1  1 kuku kuku    3    3
## 2 44 44 bubu bubu  441  441
## 3 55 55 wewe wewe 4441 4441