我有一个数据帧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 .....
如何做到这一点,请指教?
答案 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")
使用rep
和strsplit
(名称)的组合:
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