-2

I have a table like this:

enter image description here

I have two teams. I want to award 1 point depending on the KPI:

  • for baseball the point is awarded if greater or equal than goal.
  • for basketball the point is awarded if less than goal.

How can I create a Score column that calculates that? Any ideas?

user284437
  • 129
  • 5
  • you might want to check the mutate function from dplyr for exemple, or [this](https://stackoverflow.com/questions/15016723/create-categories-by-comparing-a-numeric-column-with-a-fixed-value) thread – Mayeul sgc Nov 02 '20 at 15:14
  • Does this answer your question? [Create categories by comparing a numeric column with a fixed value](https://stackoverflow.com/questions/15016723/create-categories-by-comparing-a-numeric-column-with-a-fixed-value) – Mayeul sgc Nov 02 '20 at 15:14

1 Answers1

1

You can use 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 )

Result:

df
  Teams January        KPI Goal Score
1     A      70   Baseball   80     0
2     B      50 Basketball   60     1
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34