Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...