【问题标题】:R: Replace all Values that are not equal to a set of valuesR:替换所有不等于一组值的值
【发布时间】: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 时,它不匹配任何模式。

无论如何,必须有一种更简单的方法来做到这一点。感谢您的任何/所有帮助。

【问题讨论】:

    标签: r replace dplyr


    【解决方案1】:

    这是一种 data.table 方法。

    首先,一些示例数据,因为您没有提供任何数据。

    customer <- sample(paste0("Customer",1:300),5000,replace = TRUE)
    orderData <- data.frame(customer = sample(paste0("Customer",1:300),5000,replace = TRUE),stringsAsFactors = FALSE)
    orderData <- cbind(orderData,matrix(runif(0,100,n=5000*30),ncol=30))
    out <- c("Customer123", "Customer124", "Customer125", "Customer126", "Customer127",  "Customer128","Customer140")
    
    library(data.table)
    setDT(orderData)
    result <- orderData[!(customer %in% out),customer := gsub("Customer","Supported Customer ",customer)]
    result
                        customer        1        2         3        4         5         6        7        8         9
       1: Supported Customer 134 65.35091  8.57117 79.594166 84.88867 97.225276 84.563997 17.15166 41.87160  3.717705
       2: Supported Customer 225 72.95757 32.80893 27.318046 72.97045 28.698518 60.709381 92.51114 79.90031  7.311200
       3: Supported Customer 222 39.55269 89.51003  1.626846 80.66629  9.983814 87.122153 85.80335 91.36377 14.667535
       4: Supported Customer 184 24.44624 20.64762  9.555844 74.39480 49.189537 73.126275 94.05833 36.34749  3.091072
       5: Supported Customer 194 42.34858 16.08034 34.182737 75.81006 35.167769 23.780069 36.08756 26.46816 31.994756
      ---      
    

    【讨论】:

    • 谢谢您,这非常有效。我的用例比我在 OP 中指定的要复杂一些。但是,我能够使用您的语法并进行一些修改。 orderData
    猜你喜欢
    • 2013-12-21
    • 2019-05-30
    • 2013-12-30
    • 1970-01-01
    • 2017-12-11
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多