如何在第三列的基础上将两列相乘

时间:2019-04-18 21:16:47

标签: r

我希望根据某些条件将2列相乘。

我已经尝试过IF / IFElse,但可能没有正确完成。

BetData1 <- tibble(
 Date = c("18/04/19", "18/04/19", "18/04/19"),
 Nation = c("Austria","Spain","Colombia"),
 League = c("Regionalliga", "Tercera","Primera A"),
 Home = c("Stadl Paura", "Tamaraceite", "U.Magdalena"),
 Away = c("Vokermarkt", "San Fernando", "America de Cali"),
 Pick = c("Home Win", "Home Win", "Home Win"),
 Odds = c("1.28","1.49","2.22"),
 Stake = c("5","5","5"),
 Result = c("W","W","L"))
BetData1  

我要寻找的标准是,如果'Result'= W,然后是Stake * Odds,如果'Result'= L,则是Stake-Stake,并将其放入一个名为'Return'的新列中

对此有任何帮助吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找这样的东西:

library(dplyr)

BetData1 %>% 
  mutate(Return = ifelse(Result == "W", as.numeric(Stake)*as.numeric(Odds), 0))