0

I have a data frame:

mydata <- data.frame(
  x1= as.factor(1:3), 
  x2= as.factor (4:6), 
  x3= as.factor(7:9), 
  x4= as.factor (2:7), 
  x5= as.factor(1:6), 
  x6= seq(0,600,len= 600),
  x7= seq(0,1,len=600)
  )

And I want to remove some rows in this with particular conditions. I did it this way:

mydata1 <- mydata%>%
filter(x1==1, x2==4, x3==7, x4==2, x5==1)%>%
anti_join(mydata,., by=c("x1", "x2", "x3", "x4","x5","x6" "x7"))

mydata2 <- mydata1%>%
filter(x1==3, x2==6 x3==9, x4==7, x5==6)%>%
    anti_join(mydata1,., by=c("x1", "x2", "x3", "x4","x5","x6", "x7"))

There are a lot of rows that I want to remove. Is there another way to do this?

2
  • Why are you using factors here? Factors with integer levels that don't correspond to the level numbers seem like a risky and unnecessary complication. Commented Oct 29, 2016 at 8:52
  • 1
    A base R option is mydata1 <- mydata[!as.character(interaction(mydata[1:5])) %in% "1.4.7.2.1",];mydata2 <- mydata1[!as.character(interaction(mydata1[1:5])) %in% "1.6.9.7.6",] Commented Oct 29, 2016 at 8:57

1 Answer 1

2

You can combine logical tests with & for and and | for or. If you want to remove rows that meet all of the following

x1==1, x2==4, x3==7, x4==2, x5==1

Then do it like this:

filter(mydata, !(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1))

The row order is different, but the results are the same:

md1 = filter(mydata, !(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1))
identical(arrange(md1, x6), arrange(mydata1, x6))
# [1] TRUE

You can combine your conditions into one filter this way:

md2 = filter(
  mydata,
  !(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1),
  !(x1 == 3 & x2 == 6 & x3 == 9 & x4 == 7 & x5 == 6)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.