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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…