0
votes

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).

2
str(mtcars$gear) num [1:32] 4 4 4 3 3 3 3 4 4 4 ... - Gopala

2 Answers

1
votes

The variable gear in mtcars is not a factor, so I think you mean

mtcars$gear<-as.factor(mtcars$gear)

Then, try this:

library(dplyr)
mtcars %>%
  group_by(cyl) %>%
  filter(nlevels(.$gear) > 1) %>%
  mutate(Levels = 1)

I am not sure by your question if this is what you're after, but at least it does not return an error.

And to be clear, the dplyr syntax is such that you could very cleanly do

mtcars %>%
group_by(cyl) %>%
filter(nlevels(gear) > 1) %>%
mutate(Levels = 1)
1
votes
mtcars %>%
  group_by(cyl) %>%
  mutate(Levels = ifelse(nlevels(as.factor(gear)) > 1, 1, 0))