【问题标题】:which statement with multiple conditions to create new values in existing column在现有列中创建新值的具有多个条件的语句
【发布时间】:2020-10-07 14:14:05
【问题描述】:

我想根据某些条件使用新的附加值修改现有列。请看下面的例子:

数据集:

description <- c("x value", "y value", "period 01-08-2019 t/m 31-08-2019 faktnr", "x value", "this is a sentence deb nr", "x value", "also a sentence debnr", "deb nr", "y value", "y value")
category_name <- c("x", "y", "", "x", "", "x", "", "", "y", "y")
amount<- c(-100, 200, -200, 10, 50, -3, -500, 100, 1, 1)
FullData_Cleaned <- cbind(description, category_name, amount)

我想做的是根据模式、数量以及该行是否已有值在此处应用多个条件。如果满足条件,我想填写值“credit”(见下文)

根据上述标准,这应该是输出:

("x", "y", "credit", "x", "", "x", "credit", "", "y", "y")

这是我的代码:

patterns <- c("debnr", "deb nr", "deb.nr",  "fcnr", "factnr", "factno", "faktnr")

FullData_Cleaned$category_name <- FullData_Cleaned[which(FullData_Cleaned$description %in% "patterns" & FullData_Cleaned$amount < 0 & FullData_Cleaned$category_name == ""), ] <- "credit"

但是,这行代码用“credit”而不是空白值填充整个列。

有没有人可以帮助我?

【问题讨论】:

    标签: r


    【解决方案1】:

    这就是你所追求的吗?这是dplyr 中的解决方案,而不是base-R 中的解决方案。我发现语法比 base-R 更直观,更容易执行更复杂的分析。

    首先,您需要将矩阵转换为 data.frame。然后,dplyr::mutate 函数根据ifelse 函数中的逻辑替换现有列,如果逻辑满足,则将其替换为“credit”,或者 category_name 行中的任何内容。

    library(dplyr)
    FullData_Cleaned = as.data.frame(FullData_Cleaned)
    FullData_Cleaned %>% 
        dplyr::mutate(category_name = 
            ifelse(stringr::str_detect(description, paste(patterns, collapse="|")) & amount < 0 & category_name == "", 
                "credit", 
                category_name)
            )
    
                                    description category_name amount
     1:                                 x value             x   -100
     2:                                 y value             y    200
     3: period 01-08-2019 t/m 31-08-2019 faktnr        Credit   -200
     4:                                 x value             x     10
     5:               this is a sentence deb nr                   50
     6:                                 x value             x     -3
     7:                   also a sentence debnr        Credit   -500
     8:                                  deb nr                  100
     9:                                 y value             y      1
    10:                                 y value             y      1
    

    【讨论】:

    • 这正是我想要的,非常感谢!
    • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受,以便将来帮助其他用户。
    【解决方案2】:

    这有帮助吗?数据表解决方案。首先,我使用paste 折叠模式。然后我使用like 函数将descriptionpatterns 进行比较。然后将您的 df 转换为 data.table。在那里,您可以仅选择满足条件的行(在逗号之前)并仅更改这些行的列值(在逗号之后)。

    library(data.table)
    library(dplyr)
    patterns <- paste(patterns, collapse = "|")
    FullData_Cleaned <- as.data.table(FullData_Cleaned)
    FullData_Cleaned[amount < 0 & like(vector = description, pattern = patterns, ignore.case = TRUE) == TRUE, category_name := "Credit"]
    

    输出:

                                    description category_name amount
     1:                                 x value             x   -100
     2:                                 y value             y    200
     3: period 01-08-2019 t/m 31-08-2019 faktnr        Credit   -200
     4:                                 x value             x     10
     5:               this is a sentence deb nr                   50
     6:                                 x value             x     -3
     7:                   also a sentence debnr        Credit   -500
     8:                                  deb nr                  100
     9:                                 y value             y      1
    10:                                 y value             y      1
    

    【讨论】:

    • 谢谢!这也有帮助:)
    【解决方案3】:

    我假设您有一个数据框。在这种情况下,您需要grepl 而不是%in%,因为您没有完全匹配,而是在字符串中寻找模式,即

    df$category_name[(grepl(paste(patterns, collapse = '|'), df$description)) &
                     (df$amount < 0) & 
                     (df$category_name == "")] <- "credit"
    

    注意:您需要先转换为data.frame(),然后将因子转换为字符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      相关资源
      最近更新 更多