如何将基于相等长度的数据帧列拆分为R中的单独行

时间:2020-04-20 08:32:33

标签: r strsplit

enter image description here

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

这是一个基本的R选项

f <- function(v) Map(intToUtf8,split(utf8ToInt(v),ceiling(seq(nchar(v))/2)))
q <- Vectorize(f)(mydf$V2)
p <- rep(mydf$V1,lengths(q))
mydfout <- data.frame(V1 = p,V2 = unlist(q),row.names = NULL)

给出

> mydfout
   V1 V2
1   1 ab
2   1 cd
3   1 ef
4   2 ab
5   2 cd
6   3 bg
7   3 hj
8   4 kl
9   5 ui
10  5 lm

数据

mydf <- structure(list(V1 = 1:5, V2 = c("abcdef", "abcd", "bghj", "kl", 
"uilm")), class = "data.frame", row.names = c(NA, -5L))