Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:27:37+00:00 2026-06-02T23:27:37+00:00

I have a data frame. I’m trying to create a dummy variable that is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:27:38+00:00Added an answer on June 2, 2026 at 11:27 pm

    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)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with columns that, when concatenated (row-wise) as a string,
I have a data.frame that looks like this: ID Date.A Date.B Variable A 01/01/2012
I have a data frame that looks as follows (8 columns - the myPOSIX
I have a data frame with several factor columns containing NaN 's that I
I have a data.frame, d1, that has 7 columns, the 5th through 7th column
I have a data frame that looks like this, with two key columns and
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame composed of numeric and non-numeric columns. I would like
I have a data frame in R, and I'd like to add dummy variables
I have a data frame of integers that is a subset of all of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.