有条件地计算 R 中的列

Conditionally calculate column in R

我有一个 table 这样的:

我有两个团队。我要根据KPI奖励1分:

如何创建一个用于计算该值的分数列?有什么想法吗?

您可以使用 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