Search code examples
rstringsplitcastingnumeric

Split string of concatenated digits into a numeric vector of individual values


Suppose I have a long string of concatenated digits, 0 and 1:

"1010101010101010100011011"

I would like to split the individual values and separate them by a , like so "1,0,1,0,1,0,1,1,...". Then transform into a numeric vector like this:

c(1,0,1,0,1,...)

What would be a smart way to do that ?


Solution

  • a <- "1010101010101010100011011"
    
    b <- unlist(strsplit(a,""))
    
    c <- as.numeric(b)
    
    class(c)
    #> [1] "numeric"