【发布时间】:2021-06-02 10:39:03
【问题描述】:
我必须对大型数据表(+30m 行)(实际上很多)做一些正则表达式。其中一列要么只是一个重复的字符串(每一行都相同或缺失),而另一列是每行不同的字符串。现在,如果第一列值丢失或传递了其他一些正则表达式,我不想执行正则表达式并只返回 FALSE,如果它没有丢失,我想查看列是否匹配。这是因为我确实需要它来处理数千个 data.tables,并且因为正则表达式需要几秒钟,所以我想包含一个 ifelse 语句,如果该语句为 FALSE,则甚至不会尝试正则表达式。
这是我尝试过的,但这些都不起作用(我也尝试过 fifelse 和 if_else
library(data.table)
set.seed(10)
data_table_test <-
data.table(col = rep("c", 1e6),
col2 = paste(
sample(letters, 1e6,
replace = T),
sample(letters, 1e6,
replace = T),
sep = ""
))
data_table_test2 <-
data.table(col = rep(NA, 1e6),
col2 = paste(
sample(letters, 1e6,
replace = T),
sample(letters, 1e6,
replace = T),
sep = ""
))
data_table_test[, ':='(matching_letter_1 = stringi::stri_detect_fixed(col2, col),
matching_letter_2 = ifelse(is.na(data_table_test[1, col ]), F, stringi::stri_detect_fixed(col2, col))),]
data_table_test2[, ':='(matching_letter_1 = stringi::stri_detect_fixed(col2, col),
matching_letter_2 = ifelse(is.na(data_table_test2[1, col ]), F, stringi::stri_detect_fixed(col2, col))),]
这确实有效,但速度较慢
data_table_test2[, ':='(matching_letter_1 = stringi::stri_detect_fixed(col2, col)), ][, ':='(matching_letter_1 = fifelse(is.na(matching_letter_1), F, matching_letter_1)), ]
编辑 预期的输出应该是这样的
data_table_test[matching_letter_1 == TRUE]
应该和
一样data_table_test[matching_letter_2 == TRUE]
和
data_table_test2[matching_letter_1 == TRUE]
应该和(都是空的data.tables)一样
data_table_test2[matching_letter_2 == TRUE]
一个缓慢但实用的 tidyverse 解决方案是这样的:
data_table_test %>%
as_tibble() %>%
rowwise() %>%
mutate(matching_letter = ifelse(is.na(data_table_test$col[1]), F, stringi::stri_detect_fixed(col2, col))) %>%
filter(matching_letter)
# A tibble: 75,772 x 3
# Rowwise:
col col2 matching_letter
<chr> <chr> <lgl>
1 c cb TRUE
2 c ce TRUE
3 c yc TRUE
4 c ch TRUE
5 c ic TRUE
6 c gc TRUE
7 c cg TRUE
8 c lc TRUE
9 c ci TRUE
10 c zc TRUE
# ... with 75,762 more rows
和
data_table_test2 %>%
as_tibble() %>%
rowwise() %>%
mutate(matching_letter = ifelse(is.na(data_table_test2$col[1]), F, stringi::stri_detect_fixed(col2, col))) %>%
filter(matching_letter)
# A tibble: 0 x 3
# Rowwise:
# ... with 3 variables: col <lgl>, col2 <chr>, matching_letter <lgl>
编辑 2 这段代码可以解决问题,但不是我需要的解决方案,因为我需要测试许多列组合。我需要 data.table 操作中的 if 语句
if(is.na(data_table_test[1, col ])){
data_table_test[, matching_letter := F, ]
}else{
data_table_test[, matching_letter_1 := stringi::stri_detect_fixed(col2, col),]
}
【问题讨论】:
标签: r data.table