Split string based on alternating character in R

后端 未结 9 430
醉话见心
醉话见心 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:29

    How about this:

    s <- "111110000011110000111000"
    
    spl <- strsplit(s,"10|01")[[1]]
    l <- length(spl)
    sapply(1:l, function(i) paste0(spl[i],i%%2,ifelse(i==1 | i==l, "",i%%2)))
    
    # [1] "11111" "00000" "1111"  "0000"  "111"   "000"  
    

提交回复
热议问题