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 is 100 X 100. There are 30 columns
I have a data frame that looks as follows (8 columns - the myPOSIX
I have a data frame and I'm trying to take a factor variable and
I have a data.frame, d1, that has 7 columns, the 5th through 7th column
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data frame with two columns. First column contains categories such as
I have a data frame of integers that is a subset of all of
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have a data frame where each line represents an individual. That data frame

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.