Find max and min value on a dataframe, ignoring NAs
Asked Answered
G

3

5

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?

Gaultiero answered 23/5, 2021 at 19:57 Comment(0)
P
3

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

Putnem answered 23/5, 2021 at 19:58 Comment(0)
L
3

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)
Loader answered 23/5, 2021 at 21:53 Comment(0)
K
1

Another base R option

> range(na.omit(as.numeric(unlist(df))))
[1] 1 7

If it is factor class, you should use (thank @akrun's comment)

as.numeric(as.character(unlist(df)))
Kendo answered 23/5, 2021 at 21:39 Comment(1)
@Putnem Thanks a lot. Added your comments to the answer.Kendo

© 2022 - 2024 — McMap. All rights reserved.