How to split a number into digits in R

后端 未结 7 614
花落未央
花落未央 2021-02-01 17:38

I have a data frame with a numerical ID variable which identify the Primary, Secondary and Ultimate Sampling Units from a multistage sampling scheme. I want to split the origina

7条回答
  •  遥遥无期
    2021-02-01 18:23

    You could use for example use substring:

    df <- data.frame(ID = c(501901, 501902))
    
    splitted <- t(sapply(df$ID, function(x) substring(x, first=c(1,2,4), last=c(1,3,6))))
    cbind(df, splitted)
    #      ID 1  2   3
    #1 501901 5 01 901
    #2 501902 5 01 902
    

提交回复
热议问题