This is the data I have,
v11 <- c("00240031", "00310028")
v12 <- c("00000000", "00000000")
v13 <- c("00310064", "00180058")
data <- data.frame(v11, v12, v13)
data <- lapply(data, as.character)
testdata <- as.data.frame(data, stringsAsFactors = F)
testdata[testdata == '0'] <- '000000000'
testdata
I want to split every column (starts from v11 to v99) into two columns. I am using substr to split it like this for the first column,
transform(v11, v11_a = substr(v11, 1, 4), v11_b = substr(v11, 5, 8))
X_data v11_a v11_b
1 00240031 0024 0031
2 00310028 0031 0028
Looks fine except the X_data column. I don't want to have it in the output. Any better way to do that?
tidyr::separate won't be applicable as my data is character type?
v11 %>% separate(v11, into = c('v11_a', 'v11_b'), sep = 4)
Error in UseMethod("separate_") :
no applicable method for 'separate_' applied to an object of class "character"
Secondly, how can I repeat the process for the subsequent columns (e.g. v11 to v99)?
Ideally, after splitting and then converting to numeric type my final data should look like the this,
> dataf
v11_a v11_b v12_a v12_b v13_a v13_b
1 24 31 0 0 31 64
2 31 28 0 0 18 58
Comment:
It is amazing how quickly you are coming up with amazing solutions. Thank you all.