neversaint
neversaint

Reputation: 64074

How to remove rows where columns satisfy certain condition in data frame

I have a data frame that looks like this

df <- data.frame(cbind(1:10, sample(c(1:5), 10, replace=TRUE)))
# in real case the columns could be more than two
# and the column name could be anything.

What I want to do is to remove all rows where the value of all its columns is smaller than 5. What's the way to do it?

Upvotes: 0

Views: 2354

Answers (2)

IRTFM
IRTFM

Reputation: 263481

First of all ...please stop using cbind to create data.frames. You will be sorry if you continue. R will punish you.

df[ !rowSums(df <5) == length(df), ]

(The length() function returns the number of columns in a dataframe.)

Upvotes: 1

Juli&#225;n Urbano
Juli&#225;n Urbano

Reputation: 8488

df[!apply(df,1,function(x)all(x<5)),]

Upvotes: 3

Related Questions