I have four variabes (length1, length2, length3 and length4) and would like to add a fifth column o my dataframe which contains the maximum values of the four lengths at each row. When I run
length <- data.frame(length1, length2, length3, length4)
length$maximum <- apply(length, 1, max, na.rm = TRUE)
I obtain some -Inf values. I guess this happens in those rows where all the variables have NA values. What could I do to replace the -Inf values in the length$maximum variable with NAs? I have tried:
my.max <- function(x) ifelse( !all(is.na(x)), max(x, na.rm=T), NA)
length$maximum <- apply(length, 1, my.max, na.rm = TRUE)
But it does not seem to work.
-Inf
appears iff there are onlyNA
s and you want to set these values toNA
, just dolength$maximum[!is.finite(length$maximum)] <- NA
– Jonas