I have a character column in my dataframe that looks like
df<-
data.frame(a=c("AaBbCC","AABBCC","AAbbCC"))#df
a
1 AaBbCC
2 AABBCC
3 AAbbCC
I would like to split this column every two characters. So in this case I would like to obtain three columns named VA,VB,VC
.
I tried
library(tidyr)
library(dplyr)
df<-
data.frame(a=c("AaBbCC","AABBCC","AAbbCC"))%>%
separate(a,c(paste("V",LETTERS[1:3],sep="")),sep=c(2,2))
VA VB VC
1 Aa BbCC
2 AA BBCC
3 AA bbCC
but this is not the desired result. I like to have the result that is now in VC
split into VB
(all letter B) and VC
(all letter C)How do I get R to split every two characters. The length of the string in the column is always the same for every row (6 in this example).
I will have strings that are of length >10.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…