根据列中是否存在现有列的值来替换它

时间:2018-02-01 21:59:11

标签: r if-statement dataframe in-operator

我有一个示例数据框:

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%(&#34; Income&#34;,&#34; Cost&#34;)那么我希望新列选择列V3,如果不在列表中则选择V2。

我在考虑使用%in%函数,但不确定如何在此列表中执行if,然后获取此列以获取此列逻辑。

非常感谢帮助。 的问候,

3 个答案:

答案 0 :(得分:3)

这是你正在寻找的吗?

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)

答案 1 :(得分:2)

dat$NewCol <- ifelse(dat$V2 %in% c('Income','Cost'), dat$V3, dat$V2)
  • 您也可以使用dplyr / data.table
  • ifelse没有最佳表现。此外,您无法进行NA感知比较(即使用NA子句进行三向选择)

答案 2 :(得分:1)

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