Say we have a data.frame
with different data types, not just numbers but perhaps text as well:
UNI <- data.frame(text = c("a", "b", "c"), x = c(1, NA, NaN), y = 1:3)
Now I want to filter rows where any of the values in the numerical columns are non-numeric, i.e. NA
or NaN
or similar?
First I tried this:
UNI[!complete.cases(UNI),]
but that only works on NA
s, not NaN
s. Then I came up with this:
UNI[apply(UNI, 1, function (x) any(!is.finite(x))),]
but this will fail, if there are any text columns, because then the data.frame gets all converted to text first!
So I had to do this:
UNI[apply(UNI[,sapply(UNI, is.numeric)], 1, function (x) any(!is.finite(x))),]
# text x y
# 2 b NA 2
# 3 c NaN 3
which gives correct answer, but is pretty clumsy to my taste. Especially the part where I had to filter out just the numerical columns. Isn’t there something more neat and short, e.g. in tidyverse or dplyr or similar, where I don’t have to do it in such a complicated way?
3
Yes, the tidyverse package (specifically dplyr) offers a more elegant and readable way to handle this.
library(dplyr)
UNI %>%
filter(if_any(where(is.numeric), ~ !is.finite(.)))