Imagine I have a data.frame
with many columns in R. I would like to select rows only where all of the columns have finite values.
set.seed(123)
d = data.frame(matrix(sample(c(1:10, Inf, -Inf), 100, replace=T), ncol=20))
I don't want to refer to each column by name since there are a lot of them. na.omit
and complete.cases
wont' do the trick here.
One way to do it is to run:
d[apply(apply(d, 2, is.finite), 1, all),]
This is ugly. Is there a better way?
How about:
d[is.finite(rowSums(d)), ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With