嵌入式Ifelse声明

时间:2017-03-02 19:29:36

标签: r if-statement dplyr dcast

我有一个数据集如下:

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_Diff”的金额小于0,则打印“成本”, 如果“Price_Diff”的金额小于0,则打印“价格”, 如果“Sales_Diff”的金额小于0,则打印“Sales”, 否则打印“我们起来了”。

最终输出:

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)  

1 个答案:

答案 0 :(得分:1)

我无法完全了解您希望如何构建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语句后停止,因此无法打印出多个&#34; Down原因&#34;就像你在Source D中所做的那样。如果你真的想要打印所有内容,我认为你应该能够添加4个新列来检查每个Rev,Cost,Price,Sales,并添加第5列来总结所有内容。 / p>

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