4

I have a data frame. I'm trying to create a dummy variable that is the maximum of 3 columns for a given row.

for(i in 1:nrow(data))
{
  data[i,]$max_metric <- max(data[i,]$a,
                             data[i,]$b,
                             data[i,]$c)
}

This code works, but it's definitely not the best way to do it. Are there any other ways to do this?

1 Answer 1

5

Use pmax, which takes the element-wise maximum of all arguments passed to it. However, this means you can't just pass the whole data.frame.

# this won't work because data[,c("a","b","c")] is one argument
data$max_metric <- pmax(data[,c("a","b","c")])

But you can pass each column of the data.frame to pmax via do.call because the second argument to do.call should be a list and data.frames are lists (with some attributes).

data$max_metric <- do.call(pmax, data[,c("a","b","c")])
# if you want na.rm=TRUE
data$max_metric <- do.call(pmax, c(data[,c("a","b","c")],list(na.rm=TRUE)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.