使用来自另一列的位置在数据表中子集字符串列

时间:2017-04-12 22:04:20

标签: r data.table stringr

我有一个数据表,其中包含以下类型的多个列:

   attr1 attr2
1: 01001 01000
2: 11000 10000
3: 00100 00100
4: 01100 01000

DT = setDT(structure(list(attr1 = c("01001", "11000", "00100", "01100"), 
    attr2 = c("01000", "10000", "00100", "01000")), .Names = c("attr1", 
"attr2"), row.names = c(NA, -4L), class = "data.frame"))

所有列都是字符串而不是数字。 我想要实现的目标如下:

1)我想找到" 1"出现在attr1

的字符串中

2)在这些位置取attr2的值

我在这种情况下的结果是:

[1] "10" "10" "1"  "10"

作为第一行的一个例子,attr1有" 1"在第2和第5位,我将attr2的第一行分配到第2和第5位,最后以" 10"结束。

我想要做的就是对列进行修改,然后使用它,但我真的希望有更好的方法。

3 个答案:

答案 0 :(得分:9)

您可以在@ alistaire的regmatches答案中使用变体,因为还有替换函数regmatches<-。因此,不要提取1值,而是将0值替换为""

dt[, matches := `regmatches<-`(attr2, gregexpr("0+", attr1), value="")]

#   attr1 attr2 matches
#1: 01001 01000      10
#2: 11000 10000      10
#3: 00100 00100       1
#4: 01100 01000      10

您对strsplit的想法和比较也是可行的:

dt[, matches := mapply(function(x,y) paste(y[x==1],collapse=""), strsplit(attr1,""), strsplit(attr2,""))]

答案 1 :(得分:7)

您可以使用基地R regmatches提供不同的字符串进行匹配和替换:

dt[, matches := sapply(regmatches(attr2, gregexpr('1+', attr1)), paste, collapse = '')][]
#>    attr1 attr2 matches
#> 1: 01001 01000      10
#> 2: 11000 10000      10
#> 3: 00100 00100       1
#> 4: 01100 01000      10

数据

dt <- structure(list(attr1 = c("01001", "11000", "00100", "01100"), 
        attr2 = c("01000", "10000", "00100", "01000")), .Names = c("attr1", 
    "attr2"), row.names = c(NA, -4L), class = "data.frame")

setDT(dt)

答案 2 :(得分:3)

一直在努力,然后看到@latemail评论

library(data.table)
DT = setDT(structure(list(attr1 = c("01001", "11000", "00100", "01100"),
    attr2 = c("01000", "10000", "00100", "01000")), .Names = c("attr1",
        "attr2"), row.names = c(NA, -4L), class = "data.frame"))

set.seed(0L)
N <- 1e5
dt <- data.table(attr1=do.call(paste0, data.frame(matrix(sample(0:1, N*5, replace=TRUE), ncol=5))),
    attr2=do.call(paste0, data.frame(matrix(sample(0:1, N*5, replace=TRUE), ncol=5))))

func_woSapply <- function() {
    dt1 <- copy(dt)
    dt1[, matches := `regmatches<-`(attr2, gregexpr("0+", attr1), value="")]
    dt1
}

func_withSapply <- function() {
    dt2 <- copy(dt)
    dt2[, matches := sapply(regmatches(attr2, gregexpr('1+', attr1)), paste, collapse = '')]
    dt2
}

func_useLogical <- function() {
    dt3 <- copy(dt)
    dt3[, matches := {
            d <- lapply(.SD, strsplit, "")
            lapply(mapply(function(x, y) y[as.logical(as.numeric(x))],
                d[["attr1"]], d[["attr2"]], SIMPLIFY=TRUE), paste, collapse="")
        }]
    dt3
}

library(stringi)
func_stringi <- function() {
    dt4 <- copy(dt)
    dt4[, matches := stri_c_list(Map(stri_sub, attr2, stri_locate_all_regex(attr1, '1+')))]
    dt4
}

func_indexing <- function() {
    dt[, mapply(function(x,y) paste(y[x==1],collapse=""), strsplit(attr1,""), strsplit(attr2,""))]
}

library(microbenchmark)
microbenchmark(
    func_woSapply=func_woSapply(),
    func_withSapply=func_withSapply(),
    func_useLogical=func_useLogical(),
    func_stringi=func_stringi(),
    func_indexing=func_indexing(),
    times=10L)

# Unit: milliseconds
#            expr       min        lq      mean    median        uq       max neval
#   func_woSapply 2167.8063 2167.8063 2167.8063 2167.8063 2167.8063 2167.8063     1
# func_withSapply 1693.5539 1693.5539 1693.5539 1693.5539 1693.5539 1693.5539     1
# func_useLogical 1317.5950 1317.5950 1317.5950 1317.5950 1317.5950 1317.5950     1
#    func_stringi  598.3469  598.3469  598.3469  598.3469  598.3469  598.3469     1
#   func_indexing  816.8548  816.8548  816.8548  816.8548  816.8548  816.8548     1
相关问题