Stack Exchange
R: case_when and filter combination resulting in undefined column select error
I am having trouble using a combination of the case_when and filter functions in a loop from the dplyr package in R.
I have the following code (data is a dataframe that contains a column named kind of type double):
groupkind <- c('club', 'team', 'community')

for(k in groupkind) {
  data_groupkind <- case_when(
    k == 'club' ~ filter(data, kind %in% c(2,4)),
    k == 'team' ~ filter(data, kind %in% c(3,5,6,11)),
    k == 'community' ~ filter(data, kind == 0))
  )
}

Of course I am doing many other things in the loop, but this is the section of code that is problematic.
While running this piece of code, I get the following error straight from the first iteration: Error in [.data.frame(value[[1]], rep(NA_integer_, m)) : undefined columns selected.
The odd thing is that when I run filter(data, kind %in% c(2,4)) outside the loop I don't get any error. And the case_when function works properly as well when I assign data_groupkind to anything else inside the loop.
So am I missing something here, or is it a bug from dplyr?
Thanks in advance for your answers.

case_when is not designed to return dataframe/tibbles. Use switch or better simple if/else.
library(dplyr)

for(k in groupkind) {
  data_groupkind <- if(k == 'club') filter(data, kind %in% c(2,4)),
                    else if(k == 'team') filter(data, kind %in% c(3,5,6,11)),
                    else if(k == 'community') filter(data, kind == 0)
}


Oh ok, I guess I should have been more careful about reading doc. I ended up using the simple ifelse solution of course, but I was wondering wether case_when was behaving in a way it shouldn't. Anyway thanks for the clarification
