Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max and min value on a dataframe, ignoring NAs

Tags:

r

max

na

min

operation

I need to find the max and min value in a dataframe "df" like this:

col1  col2  col3 
 7      4    5
 2      NA   6
 3      2    4
 NA     NA   1

The result should be: min = 1 and max = 7. I have used this function:

min <- min(df, na.rm=TRUE)
max <- max(df, na.rm=TRUE)

but it gives me the following error:

 Error in FUN(X[[i]], ...) : 
  only defined on a data frame with all numeric variables 

So I have converted all the values as.numeric in this way:

df <- as.numeric(as.character(df))

but it introduces NAs by coercion and now the results are: min = -Inf and max=Inf

How can I operate on the df ignoring NAs?

like image 208
EmaK Avatar asked Dec 31 '22 14:12

EmaK


2 Answers

If the columns are not numeric, convert it with type.convert

df <- type.convert(df, as.is = TRUE)

Or use a force conversion with matrix route

df[] <- as.numeric(as.matrix(df))

Or with lapply

df[] <- lapply(df, function(x) as.numeric(as.character(x)))

With R 4.1.0 we can also do

sapply(df, \(x) as.numeric(as.character(x))) |> 
   range(na.rm = TRUE)
#[1] 1 7

Once the columns are numeric, the functions work as expected

min(df, na.rm = TRUE)
#[1] 1
max(df, na.rm = TRUE)
#[1] 7

Note that as.character/as.numeric requires a vector input and not a data.frame

like image 96
akrun Avatar answered Jan 18 '23 13:01

akrun


We could use minMax function from dataMaid package (handles NA's)

library(dataMaid)

minMax(df, maxDecimals = 2)

Output:

Min. and max.: 2; 7

data:

df <- tribble(
~col1,  ~col2,  ~col3, 
7,      4,    5,
2,      NA,   6,
3,      2,    4,
NA,     NA,  1)
like image 38
TarJae Avatar answered Jan 18 '23 13:01

TarJae