4

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?

EmaK
  • 154
  • 1
  • 9

3 Answers3

4

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

akrun
  • 874,273
  • 37
  • 540
  • 662
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)
TarJae
  • 72,363
  • 6
  • 19
  • 66
2

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)))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81