Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select only rows with finite values in data.frame R

Tags:

dataframe

r

apply

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?

like image 273
Alex Avatar asked Mar 23 '23 09:03

Alex


1 Answers

How about:

d[is.finite(rowSums(d)), ]
like image 200
eddi Avatar answered Apr 02 '23 10:04

eddi