Subset dataframe based on rowSums

I am trying to drop all rows from my dataset for which the sum of rows over multiple columns equals a certain number.

It looks something like this:

a <- c(1,1,1,1,1,1)
b <- c(1,1,1,1,1,1)
e <- c(0,1,1,1,1,1)

d <- data.frame(a,b,e)    

d_subset <- d[!rowSums(d[,2:3], na.rm=T) == 1]

So d_subset should contain all rows where the sum of column 2 and 3 does not equal 1. Essentially, in this example, the first row should be dropped. However, this is not working and I am not sure why.

Upvotes: 1

Views: 413

Answers (3)

Quinten
Quinten

Reputation: 41437

You can also just use this:

d[rowSums(d[2:3]) != 1, ]

Output:

  a b e
2 1 1 1
3 1 1 1
4 1 1 1
5 1 1 1
6 1 1 1

Upvotes: 1

jpsmith
jpsmith

Reputation: 17450

You are missing a comma in your current code, do:

d_subset <- d[!rowSums(d[,2:3], na.rm=T) == 1,]

Output:

#  a b e
#2 1 1 1
#3 1 1 1
#4 1 1 1
#5 1 1 1
#6 1 1 1

Upvotes: 3

akrun
akrun

Reputation: 887501

We can do

subset(d, rowSums(d[2:3]) !=1)

Upvotes: 2

Related Questions