Andi Maier
Andi Maier

Reputation: 1034

dplyr filter_at command with missing values


I have a question on R and the dplyr command. I have got the following data frame df:

  ID   bank  type
  1    own   online
  2    own   offline
  3    NaN   total   

Now I want to filter on the NaN value in combination with type "total". I know that I could easily filter on the bank column only but I would be interested in combining two columns where one is NaN. My command does not work

vec <- c("total)
l1 <- df %>% filter_at(vars(type,bank), any_vars(!(.  %in% vec) && (!is.na(.))))
nrow(l1)

The output should be "1".

Many thank in advance!

Upvotes: 0

Views: 242

Answers (2)

akrun
akrun

Reputation: 886938

Here is an option with data.table

library(data.table)
setDT(df)[is.na(bank) & type %in% vec]

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388807

Since you want to use two different functions on different columns you should use filter here :

library(dplyr)
df %>% filter(is.na(bank) & type  %in% vec)

which can also be done in base R :

subset(df, is.na(bank) & type  %in% vec)

Upvotes: 1

Related Questions