Greg
Greg

Reputation: 1070

Create column based on rows of dataframe

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?

Upvotes: 4

Views: 297

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176638

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

Upvotes: 5

Related Questions