Search code examples
rdplyrtidyverse

Create a column such that - If `x` lies in top 150, its value is "Good". Else its value is "Bad"


How do I create a binary column performance in tbl such that -

If x lies in top 150 (our of 243), value of performance is "Good"

Else, value of performance is "Bad"

tbl <- tibble(x = runif(n = 243, min = 0, max = 1))


Solution

  • If i understand correctly your question this should work:

    tbl %>%
        arrange(desc(x)) %>%
        mutate(performance = case_when(
                row_number() < 150 ~ "Good",
                TRUE ~ "Bad"))