Home > Net >  Remove rows from dataframe that don't contain any numeric values
Remove rows from dataframe that don't contain any numeric values

Time:11-12

I have an example dataframe as follows:

x <- data.frame(Var1 = c(NA,Inf,7,NA), Var2 = c(1,61, -Inf, -Inf), Var3 = c(12, 31, 49, Inf))
x
 Var1 Var2 Var3
1   NA    1   12
2  Inf   61   31
3    7 -Inf   49
4   NA -Inf  Inf

I would like to remove rows from this data frame that do not have at least one numeric value. So in this example, I'd like to only remove row 4, leaving a final data frame:

Var1 Var2 Var3
1   NA    1   12
2  Inf   61   31
3    7 -Inf   49

CodePudding user response:

We may use is.finite in filter with if_any to subset data having any finite values in a row

library(dplyr)
x %>% 
   filter(if_any(everything(), is.finite))
  Var1 Var2 Var3
1   NA    1   12
2  Inf   61   31
3    7 -Inf   49
  • Related