lll lll - 1 year ago 64
R Question

R: how to calculate if any of column in each row satisfies a condition without loop

dat = runif(10,1,10)
dat2 = runif(10,1,10)
dat3 = runif(10,1,10)
data = rbind(dat,dat2,dat3)

In the case of above data, I am wondering how I can filter out rows as long as there is one element in that row exceeding 5.

I know that I can use loop to achieve this, but I am wondering if there is more succinct way to do this.

Answer Source

try this:

data[apply(data>5, 1, sum)>0,]

This says "for condition having more than zero number exceeding 5 in each row, filter data".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download