Adamm
Adamm

Reputation: 2306

Select data frame rows if specified columns are less or equal than some number

Let's have a simple data frame df:

A <- c(1,2,3,4)
B <- c(3,4,5,7)
C <- c(3,4,7,3)
df <- data.frame(A,B,C)
# A B C
#1 1 3 3
#2 2 4 4
#3 3 5 7
#4 4 7 3

I'd like to extract some rows where value is less or equal than 4 in columns 2 and 3 (B,C). So desired output:

  A B C
1 1 3 3
2 2 4 4

Of course I can do this by:

subset(df, df$B <= 4 & df$C <= 4)

or

df[df[,2] <= 4 & df[,2] <= 4,]

However I have plenty of columns, and I'd like to avoid typing all the stuff...

In addition to this, why those two solutions don't work?

df[df[,c(2,3)] <= 4,]


      A  B  C
1     1  3  3
2     2  4  4
NA   NA NA NA
NA.1 NA NA NA
NA.2 NA NA NA


df[apply(df [c('B','C')],1,function(x) x <= 4),]

    A  B  C
1   1  3  3
2   2  4  4
3   3  5  7
4   4  7  3
NA NA NA NA

Thanks!

Upvotes: 1

Views: 50

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73285

Fixes in both failing cases:

df[rowSums(df[, c(2,3)] <= 4) == 2,]  ## or df[rowSums(df[c(2,3)] <= 4) == 2,]
df[apply(df[c('B','C')] <= 4, 1, all), ]

The first one is recommended.

Upvotes: 3

Related Questions