根据现有列是否存在于列表中来替换现有列的值
Replace value of an existing column based on whether it's present in a list
我有一个示例数据框:
dat <- data.frame(V1 = c("Non Debt Balance","Non Debt Income","Non Debt Cost"),
V2 = c("Average Balance","Income","Cost"),
V3 = c("Trade","Sales Finance","Trade"))
我想根据列 V2 的值创建一个新列。如果 V2 列是 %in% ("Income","Cost") 那么我希望新列选择 V3 列,如果不在列表中则选择 V2.
我正在考虑使用 %in% 函数,但不确定我将如何着手在此列表中执行 if 然后获取此列,否则获取此列逻辑。
非常感谢您的帮助。
此致,
这是您要找的吗?
library(dplyr)
dat %>%
mutate(V4 = case_when(V2 == "Income" | V2 == "Cost" ~ V3,
TRUE ~ V2))
V1 V2 V3 V4
1 Non Debt Balance Average Balance Trade Average Balance
2 Non Debt Income Income Sales Finance Sales Finance
3 Non Debt Cost Cost Trade Trade
或者,使用您提到的 %in%
:
dat %>%
mutate(V4 = case_when(V2 %in% c("Cost", "Income") ~ V3,
TRUE ~ V2))
数据:
dat <- data.frame(V1 = c("Non Debt Balance","Non Debt Income","Non Debt Cost"),
V2 = c("Average Balance","Income","Cost"),
V3 = c("Trade","Sales Finance","Trade"),
stringsAsFactors = FALSE)
dat$NewCol <- ifelse(dat$V2 %in% c('Income','Cost'), dat$V3, dat$V2)
- 你也可以使用dplyr/data.table
ifelse
没有最佳性能。您也不能进行 NA 感知比较(即使用 NA 子句的三向选择)
data.table方式:
result <- setDT(dat)[,result_col:= ifelse(V2 %in% c('Income','Cost'), V3, V2)]
> result
V1 V2 V3 result_col
1 Non Debt Balance Average Balance Trade Average Balance
2 Non Debt Income Income Sales Finance Sales Finance
3 Non Debt Cost Cost Trade Trade
我有一个示例数据框:
dat <- data.frame(V1 = c("Non Debt Balance","Non Debt Income","Non Debt Cost"),
V2 = c("Average Balance","Income","Cost"),
V3 = c("Trade","Sales Finance","Trade"))
我想根据列 V2 的值创建一个新列。如果 V2 列是 %in% ("Income","Cost") 那么我希望新列选择 V3 列,如果不在列表中则选择 V2.
我正在考虑使用 %in% 函数,但不确定我将如何着手在此列表中执行 if 然后获取此列,否则获取此列逻辑。
非常感谢您的帮助。 此致,
这是您要找的吗?
library(dplyr)
dat %>%
mutate(V4 = case_when(V2 == "Income" | V2 == "Cost" ~ V3,
TRUE ~ V2))
V1 V2 V3 V4
1 Non Debt Balance Average Balance Trade Average Balance
2 Non Debt Income Income Sales Finance Sales Finance
3 Non Debt Cost Cost Trade Trade
或者,使用您提到的 %in%
:
dat %>%
mutate(V4 = case_when(V2 %in% c("Cost", "Income") ~ V3,
TRUE ~ V2))
数据:
dat <- data.frame(V1 = c("Non Debt Balance","Non Debt Income","Non Debt Cost"),
V2 = c("Average Balance","Income","Cost"),
V3 = c("Trade","Sales Finance","Trade"),
stringsAsFactors = FALSE)
dat$NewCol <- ifelse(dat$V2 %in% c('Income','Cost'), dat$V3, dat$V2)
- 你也可以使用dplyr/data.table
ifelse
没有最佳性能。您也不能进行 NA 感知比较(即使用 NA 子句的三向选择)
data.table方式:
result <- setDT(dat)[,result_col:= ifelse(V2 %in% c('Income','Cost'), V3, V2)]
> result
V1 V2 V3 result_col
1 Non Debt Balance Average Balance Trade Average Balance
2 Non Debt Income Income Sales Finance Sales Finance
3 Non Debt Cost Cost Trade Trade