Split string based on alternating character in R

后端 未结 9 431
醉话见心
醉话见心 2021-01-30 10:02

I\'m trying to figure out an efficient way to go about splitting a string like

\"111110000011110000111000\"

into a vector

[1] \         


        
9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 10:41

    Another approach in case, using mapply:

    x="111110000011110000111000"
    
    with(rle(strsplit(x,'')[[1]]), 
         mapply(function(u,v) paste0(rep(v,u), collapse=''), lengths, values))
    #[1] "11111" "00000" "1111"  "0000"  "111"   "000"  
    

提交回复
热议问题