0
votes

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.

1
You can use your first approach. If you know that -Inf appears iff there are only NAs and you want to set these values to NA, just do length$maximum[!is.finite(length$maximum)] <- NAJonas

1 Answers

0
votes

Your function definition - my.max works well. I would suggest adjusting the name of the function - e.g. my_max.

However, when you call your function, you've added an extra argument that isn't required. The code below should work:

# create test data
length1 <- c(1:9, NA_integer_)
length2 <- c(2:10, NA_integer_)
length3 <- c(3:11, NA_integer_)
length4 <- c(4:12, NA_integer_)

length <- data.frame(length1, length2, length3, length4)

# define function
my_max <- function(x) ifelse( !all(is.na(x)), max(x, na.rm = TRUE), NA)

length$maximum <- apply(length, 1, my_max)