【发布时间】:2020-11-02 23:22:02
【问题描述】:
【问题讨论】:
-
您可能想检查 dplyr 中的 mutate 函数作为示例,或 this 线程
标签: r if-statement conditional-statements calculated-columns
【问题讨论】:
标签: r if-statement conditional-statements calculated-columns
你可以使用ifelse:
df <- data.frame(
Teams = c("A", "B"),
January = c(70, 50),
KPI = c("Baseball", "Basketball"),
Goal = c(80, 60)
)
df$Score <- ifelse(df$KPI=="Baseball" & df$January >= df$Goal
| df$KPI=="Basketball" & df$January < df$Goal, 1, 0 )
结果:
df
Teams January KPI Goal Score
1 A 70 Baseball 80 0
2 B 50 Basketball 60 1
【讨论】: