嵌入式 Ifelse 语句

Embedded Ifelse Statement

我有一个数据集如下:

Source     Rev_Diff    Cost_Diff     Price_Diff      Sales_Diff      
     A          -10           10             11              12
     B           11          -10            -10              11
     C          -12           10             10             -11
     D          -11          -11            -10             -12

如何在以下位置添加一列:

"如果Rev_Diff小于0, 打印 "Cost" 如果 "Cost_Diff" 数量小于 0, 打印 "Price" 如果 "Price_Diff" 数量小于 0, 打印 "Sales" 如果 "Sales_Diff" 数量小于 0, 否则打印 "We're up"。

最终输出:

Source     Rev_Diff    Cost_Diff     Price_Diff      Sales_Diff      Reason Down     
     A          -10          -10             11              12      Cost (-10) 
     B           11          -10            -10              11      We're Up
     C          -12           10             10             -11      Sales (-11)
     D          -11          -11            -10             -12      Cost (-11), Price (-11), Sales (-12)  

我不太明白你想如何构建你的 if 语句,但下面可以根据前一列中的信息添加一个新列。

Source <- c("A", "B", "C", "D", "E")
Rev_Diff <- c(-10, 11, 12, 11, 10)
Cost_Diff <- c(10, -10, 10, -11, 11)
Price_Diff <- c(-11, 10, -10, 10, 10)
Sales_Diff <- c(12, 11, 11, -12, 11)

df <- data.frame(Source, Rev_Diff, Cost_Diff, Price_Diff, Sales_Diff)

df %>% 
  mutate(ReasonDown = ifelse(Rev_Diff < 0, paste("Rev", Rev_Diff),
                      ifelse(Cost_Diff < 0, paste("Cost", Cost_Diff),
                      ifelse(Price_Diff < 0, paste("Price", Price_Diff),
                      ifelse(Sales_Diff < 0, paste("Sales", Sales_Diff), "We're up")))))

  Source Rev_Diff Cost_Diff Price_Diff Sales_Diff ReasonDown
1      A      -10        10        -11         12    Rev -10
2      B       11       -10         10         11   Cost -10
3      C       12        10        -10         11  Price -10
4      D       11       -11         10        -12   Cost -11
5      E       10        11         10         11   We're up

但是,ifelse 一旦找到 TRUE 语句就会停止,所以这将无法像在源 D 中那样打印出多个 "Down Reasons"。如果你真的想打印所有内容,我想你应该能够添加 4 个新列来检查 Rev、Cost、Price、Sales 中的每一个,并添加第 5 列来总结所有内容。

df %>% 
  mutate(RRev = ifelse(Rev_Diff < 0, paste("Rev", Rev_Diff), "")) %>% 
  mutate(RCost = ifelse(Cost_Diff < 0, paste("Cost", Cost_Diff), "")) %>%
  mutate(RPrice = ifelse(Price_Diff < 0, paste("Price", Rev_Diff), "")) %>%
  mutate(RSales = ifelse(Sales_Diff < 0, paste("Sales", Rev_Diff), "")) %>% 
  mutate(DownReason = ifelse(nchar(paste(RRev, RCost, RPrice, RSales)) > 3, paste(RRev, RCost, RPrice, RSales), "We're UP"))

  Source Rev_Diff Cost_Diff Price_Diff Sales_Diff    RRev    RCost    RPrice   RSales          DownReason
1      A      -10        10        -11         12 Rev -10          Price -10          Rev -10  Price -10 
2      B       11       -10         10         11         Cost -10                             Cost -10  
3      C       12        10        -10         11                   Price 12                    Price 12 
4      D       11       -11         10        -12         Cost -11           Sales 11  Cost -11  Sales 11
5      E       10        11         10         11                                                We're UP