Reputation: 721
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.
Upvotes: 3
Views: 107
Reputation: 33498
Some playing around in data.table
and reusing your existing substr()
logic:
library(data.table)
setDT(testdata)
cols <- paste0("v", 11:13)
new_cols <- paste0(rep(cols, 2), rep(c("a", "b"), each = length(cols)))
extra <- function(x) substr(x, 1, 4)
extrb <- function(x) substr(x, 5, 8)
testdata[, (new_cols) := c(lapply(.SD, extra), lapply(.SD, extrb)), .SDcols = cols]
> testdata
v11 v12 v13 v11a v12a v13a v11b v12b v13b
1: 00240031 00000000 00310064 0024 0000 0031 0031 0000 0064
2: 00310028 00000000 00180058 0031 0000 0018 0028 0000 0058
Upvotes: 1
Reputation: 886938
In base R
, this can be done by looping through the columns, replace the 0's in between non-zero with a delimiter ,
, read into a data.frame (read.table
), and cbind
the list
of datasets
lst1 <- lapply(testdata, function(x) {
x1 <- read.table(text = sub("(?<=[1-9])0+", ",", x, perl = TRUE),
header = FALSE, sep=",", col.names = c('a', 'b'), fill = TRUE)
replace(x1, is.na(x1), 0)})
do.call(cbind, lst1)
# 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
It can be also done with tidyverse
by first gather
ing into 'long' format, then do the separate
ion, and finally spread
it back to 'wide' format
library(tidyverse)
gather(testdata) %>%
separate(value, into = c('a', 'b'), sep=4, convert = TRUE) %>%
gather(key1, val, a:b) %>%
unite(key, key, key1, sep="_") %>%
group_by(key) %>%
mutate(ind = row_number()) %>%
spread(key, val) %>%
select(-ind)
# A tibble: 2 x 6
# v11_a v11_b v12_a v12_b v13_a v13_b
# <int> <int> <int> <int> <int> <int>
#1 24 31 0 0 31 64
#2 31 28 0 0 18 58
Or another option is to use summarise_all
with read.table
testdata %>%
summarise_all(funs(list(read.table(text =sub("^(....)", "\\1 ", .),
header = FALSE)))) %>%
unnest
Upvotes: 1
Reputation: 51582
Here is an idea using the very handy for such operations library(splitstackshape)
,
library(splitstackshape)
cSplit(setDT(testdata)[, lapply(.SD, function(i) gsub("(.{4})", "\\1 ", i))], names(testdata), sep = ' ')
# v11_1 v11_2 v12_1 v12_2 v13_1 v13_2
#1: 24 31 0 0 31 64
#2: 31 28 0 0 18 58
Upvotes: 1