user3091668
user3091668

Reputation: 2310

Subset columns based on sum of rows

I would like to subset the df columns based on sum of rows.

df dataframe:

NE001  NE002  NE003  NE004
  2      0      0      2

My expected output dfo:

NE001  NE004
 2       2

I have tried dfo <- df[,which(names(df) == colSums(df==2))] but do not work.

Some ideas?

Upvotes: 1

Views: 2915

Answers (1)

Rich Scriven
Rich Scriven

Reputation: 99331

Are you just trying to subset the columns that sum to 2? If so, you were close. Here's an example with a small data frame.

(d <- data.frame(x = c(2, 0), y = c(1, 2), z = c(1, 1)))
#   x y z
# 1 2 1 1
# 2 0 2 1

Since colSums(d) == 2 returns a logical vector, we can subset with that and the columns that are TRUE will be returned

colSums(d) == 2
#    x     y     z 
# TRUE FALSE  TRUE 
d[colSums(d) == 2]
#   x z
# 1 2 1
# 2 0 1

Upvotes: 5

Related Questions