Reputation: 87
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.
Upvotes: 0
Views: 1457
Reputation: 389155
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)
}
Upvotes: 1