【发布时间】:2020-03-23 18:12:19
【问题描述】:
全部。
一段时间以来,我一直在尝试解决大型数据集上的问题,并且可以利用您的一些智慧。
我有一个 DF (1.3M obs),其中包含一个名为 customer 的列以及 30 个其他列。假设它包含客户 Customer1 到 Customer3000 的多个实例。我知道我与其中 30 位客户有问题。我需要找到所有不是我遇到问题的客户的客户,并将“客户”列中的值替换为“支持的客户”文本。这似乎应该是一件简单的事情......如果不是因为 obs 的数量,我会在 Excel 中加载它,过滤掉所有不良客户,然后将文本“支持的客户”复制/粘贴到剩下的内容上。
我尝试使用 grepl 和 paste/paste0 替换和 str_replace_all 但无济于事。我当前的代码如下所示:
#All the customers that have issues
out <- c("Customer123", "Customer124", "Customer125", "Customer126", "Customer127",
"Customer128", ..... , "Customer140")
#Look for everything that is NOT in the list above and replace with "Enabled"
orderData$customer <- str_replace_all(orderData$customer, paste0("[^", paste(out, collapse =
"|"), "]"), "Enabled Customers")
该代码让我出现此错误:
Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), :
In a character range [x-y], x is greater than y. (U_REGEX_INVALID_RANGE)
我尝试了与此方法相反的方法,并提取了与 out 客户列表不匹配的所有 obs 的列表。像这样的:
in <- orderData %>% filter(!customer %in% out) %>% select(customer) %>%
distinct(customer)
这让我获得了更多已启用的客户列表 (~3,100)。不过,使用 str_replace_all 和粘贴方法似乎有问题。在如此大量的模式下,粘贴不再使用“|”折叠操作员。相反,我得到一个看起来像这样的字符串:
"c(\"Customer1\", \"Customer2345\", \"Customer54\", ......)
当传递给 str_replace_all 时,它不匹配任何模式。
无论如何,必须有一种更简单的方法来做到这一点。感谢您的任何/所有帮助。
【问题讨论】: