I'm trying to encode a column for my dataset if there is more than one level once the data has been grouped down by a couple factors. For simplicity I am using the mtcars data set as an example. I keep getting the error "Error: filter condition does not evaluate to a logical vector. "
Clearly this syntax isn't acceptable, but does anyone have a more clever way of doing this?
df_levels <- mtcars %>%
group_by(mpg) %>%
filter(nlevels(.$gear) > 1) %>%
mutate(Levels = 1) ##encode with a boolean value indicating more than one level
and
df_levels <- df_levels %>%
group_by(mpg) %>%
filter(nlevels(.$gear) < 1) %>%
mutate(Levels = 0)
So if you click on the new df "df_levels", and sort by mpg in the df viewer, you would see a column "levels" = 0 for entries with 10.4 mpg (because there is only data with gear = 3), and the column "levels" associated with 30.4 mpg would have value = 1 because there is more than one level for gear in that data grouping (gear = 4, gear = 5).