0

First time asking a question here and I'm new to R. I'm essentially working with a data frame that looks something like this (the row is longer than this, but it's to get the idea):

Category Site
IV NA
I Hip
III NA
IVB Leg
IVA NA
II Arm

I want to remove the whole line if the row has an NA in the site AND is category IV, IVA, or IVB. I don't want to remove all of the rows with NA in Site.

This is what I've tried, but I can't seem to get it to work properly:

%>% filter(Category !=c("IV", "IVA", "IVB") & !is.na(Site))

I get this message:

Warning message: In Category != c("IV", "IVA", "IVB") : longer object length is not a multiple of shorter object length

And the resulting dataframe has removed all the rows with NA in site. I feel like I'm missing something very simple, but I can't seem to make it do what I want haha. Any help would be greatly appreciated!

Becky
  • 9
  • 4

1 Answers1

0

Use %in% to compare multiple values and I think the condition needs some correction.

library(dplyr)

df %>% filter(!(Category  %in% c("IV", "IVA", "IVB") & is.na(Site)))

#  Category Site
#1        I  Hip
#2      III <NA>
#3      IVB  Leg
#4       II  Arm

Or in base R -

subset(df, !(Category  %in% c("IV", "IVA", "IVB") & is.na(Site)))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213