将字符列分为两部分并重复

时间:2019-01-16 10:37:15

标签: r tidyverse

这是我的数据,

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

我想将每列(从v11到v99)分成两列。我正在使用substr像这样将其拆分为第一列

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

看起来不错,但X_data列除外。我不想在输出中使用它。还有更好的方法吗?

tidyr::separate将不适用,因为我的数据是字符类型?

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"

第二,如何为后续列(例如v11至v99)重复该过程?

理想情况下,在拆分然后转换为数字类型后,我的最终数据应如下所示,

> 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

评论:

令人惊讶的是,您很快就能想到出色的解决方案。谢谢你们。

3 个答案:

答案 0 :(得分:1)

base R中,这可以通过遍历各列来完成,用定界符,替换非零之间的0,读入data.frame(read.table) ,以及cbind的数据集list

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

也可以使用tidyverse来完成,方法是先gather设为“长”格式,然后执行separate ion,最后spread回到“宽” '格式

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

或者另一种选择是将summarise_allread.table一起使用

testdata %>%
   summarise_all(funs(list(read.table(text =sub("^(....)", "\\1 ", .),
             header = FALSE)))) %>%
   unnest

答案 1 :(得分:1)

这里有一个非常方便的想法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

答案 2 :(得分:1)

有些人在data.table中玩耍,并重用您现有的substr()逻辑:

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