Home > Software design >  Subset dataframe based on rowSums
Subset dataframe based on rowSums

Time:05-28

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.

CodePudding user response:

We can do

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

CodePudding user response:

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

CodePudding user response:

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
  • Related