如何有效地检查R列表元素的标识是否为向量



我有以下R data.table,其中一列是包含数字元素的列表:

library(data.table)
dt = data.table(
      numericcol = rep(42, 8),
      listcol = list(c(1, 22, 3), 6, 1, 12, c(5, 6, 1123), 3, 42, 1)
  )
> dt
   numericcol        listcol
1:         42        1,22, 3
2:         42              6
3:         42              1
4:         42             12
5:         42    5,   6,1123
6:         42              3
7:         42             42
8:         42              1

我想创建两列:(1( 显示每个列表元素大小的列和 (2( 布尔列,如果 1 是元素,则为 TRUE,否则为 FALSE。

输出应如下所示:

   numericcol        listcol     size    ones
1:         42        1,22, 3     3       TRUE
2:         42              6     1       FALSE
3:         42              1     1       TRUE       
4:         42             12     1       FALSE
5:         42    5,   6,1123     3       FALSE
6:         42              3     1       FALSE
7:         42             42     1       FALSE
8:         42              1     1       TRUE

所以,我知道如何创建列size,即

dt[, size:=sapply(dt$listcol, length)]

而且我知道如何检查如果那里只有一个数字,则带有元素的行是否有 1,即

dt[, ones := dt$listcol[dt$listcol == 1] ]

然而,这种假设是错误的。我不知道如何检查具有多个整数的列表列的行是否由 1 组成。

什么是有效的方法?

dt[, o := sapply(listcol, function(x) 1 %in% x)]
dt
#    numericcol        listcol     o
# 1:         42        1,22, 3  TRUE
# 2:         42              6 FALSE
# 3:         42              1  TRUE
# 4:         42             12 FALSE
# 5:         42    5,   6,1123 FALSE
# 6:         42              3 FALSE
# 7:         42             42 FALSE
# 8:         42              1  TRUE

我们可以通过取 'listcol' 的lengths来创建 'size',然后遍历 'listcol',检查 1 是否%in%每个vector s 并将其分配给 'ones'

dt[, size := lengths(listcol)
   ][, ones := unlist(lapply(listcol, function(x) 1 %in% x))]
dt
#   numericcol        listcol size  ones
#1:         42        1,22, 3    3  TRUE
#2:         42              6    1 FALSE
#3:         42              1    1  TRUE
#4:         42             12    1 FALSE
#5:         42    5,   6,1123    3 FALSE
#6:         42              3    1 FALSE
#7:         42             42    1 FALSE
#8:         42              1    1  TRUE

或者另一种选择是使用purrr中的map

,效率更高
library(purrr)
dt[, ones := map_lgl(listcol, `%in%`, x = 1)]

以及是否有并行处理的选项

library(furrr)
plan(multiprocess)
dt[, one := future_map_lgl(listcol, `%in%`, x = 1)]

另外,如果我们打算用tidyverse

dt %>%
   mutate(size = lengths(listcol),
          ones = map(listcol, `%in%`, x = 1))

基准

set.seed(24)
dt1 <- data.table( numericcol = rep(42,  8000000),
         listcol = rep(list(c(1, 22, 3), 6, 1, 12, c(5, 6, 1123), 3, 42, 1), 1e6))
dt2 <- copy(dt1)
#timing for creating the size column
system.time({
dt1[, size := lengths(listcol)]
})
# user  system elapsed 
#    0.3     0.0     0.3 
system.time({
dt2[, size:= sapply(listcol, length)]
})
# user  system elapsed 
#   6.45    0.28    6.97 

#timing for creating the second column
system.time({
dt1[, ones := unlist(lapply(listcol, function(x) 1 %in% x))]
})
# user  system elapsed 
#  15.12    0.26   16.42 
system.time({
dt2[, ones := sapply(listcol, function(x) any(1 %in% x))]
})
# user  system elapsed 
#  17.00    0.04   17.52 
system.time({
 dt2[, one := map_lgl(listcol, `%in%`, x = 1)]
 })
# user  system elapsed 
#  10.92    0.00   11.25 

最新更新