Select only rows with trailing values ​​in data.frame R

Imagine I have a data.frame with many columns in R. I would like to select rows only where all columns have finite values.

 set.seed(123) d = data.frame(matrix(sample(c(1:10, Inf, -Inf), 100, replace=T), ncol=20)) 

I do not want to refer to each column by name, as there are many of them. na.omit and complete.cases will not do the trick.

One way to do this:

 d[apply(apply(d, 2, is.finite), 1, all),] 

It's not beautiful. Is there a better way?

+4
source share
1 answer

What about:

 d[is.finite(rowSums(d)), ] 
+7
source

Source: https://habr.com/ru/post/1502883/


All Articles