'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 1:[1]

If i understand correctly your question this should work:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucca Nielsen