0

I am trying to get a column made up of the minimum values from other specified columns in which NA values are ignored. When I do this:

foo = data.frame(A = c(1, 2, 3, NA), B = c(3, NA, 2, NA), C = c(NA, 1, 2, 3))
foo$MIN = apply(foo[c("A", "B")], 1, min)

I get this:

   A  B  C MIN
1  1  3 NA   1
2  2 NA  1  NA
3  3  2  2   2
4 NA NA  3  NA

(Note that I'm including a superfluous "C" column to illustrate the point that I want to be able to specify columns, in this case A and B.)

The issue here is that the MIN value in row 2 is NA, when I want it to be 2. I'm happy with MIN being NA in row 4, because the A and B values there are both NA. But when choosing between 2 and NA, I want 2.

Maël
  • 45,206
  • 3
  • 29
  • 67

1 Answers1

2

You can add na.rm=T to your function and set to NAwhen the entire vector of values is NA (otherwise it returns it returns -Inf).

my_min <- function(x) ifelse( !all(is.na(x)), min(x, na.rm=T), NA)
foo$MIN = apply(foo[c("A", "B")], 1, my_min)

output:

   A  B  C MIN
1  1  3 NA   3
2  2 NA  1   2
3  3  2  2   3
4 NA NA  3  NA
Maël
  • 45,206
  • 3
  • 29
  • 67