Search code examples
rmaxminnaoperation

Find max and min value on a dataframe, ignoring NAs


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?


Solution

  • 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