Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ignore NA values when using MIN function of "apply"

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.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading