【发布时间】: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