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 7868759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:06:11+00:00 2026-06-03T01:06:11+00:00

I am trying to write some code which identifies the greatest two values for

  • 0

I am trying to write some code which identifies the greatest two values for each row and provides their column number and value.

df = data.frame( car = c (2,1,1,1,0), bus = c (0,2,0,1,0),
                 walk = c (0,3,2,0,0), bike = c(0,4,0,0,1))

I’ve managed to get it to do this for the maximum value using the max and max.col functions.

df$max = max.col(df,ties.method="first")
df$val = apply(df[ ,1:4], 1, max)

As far as I know there are no equivalent functions for the second highest value so doing this has made things a little trickier. Using this code provides the second highest value but (importantly) not in situations with ties. Also it looks risky.

sec.fun <- function (x) {
  max( x[x!=max(x)] )
}

df$val2 <- apply(df[ ,1:4], 1, sec.fun)

Ideally the solution would not involve removing any original data and could be used to find the third, fourth… highest value but neither of these are essential requirements.

  • 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-03T01:06:13+00:00Added an answer on June 3, 2026 at 1:06 am

    try this:

    # a function that returns the position of n-th largest
    maxn <- function(n) function(x) order(x, decreasing = TRUE)[n]
    

    this is a closure, so you can use like this:

    > # position of the largest
    > apply(df, 1, maxn(1))
    [1] 1 4 3 1 4
    > # position of the 2nd largest
    > apply(df, 1, maxn(2))
    [1] 2 3 1 2 1
    > 
    > # value of the largest
    > apply(df, 1, function(x)x[maxn(1)(x)])
    [1] 2 4 2 1 1
    > # value of the 2nd largest
    > apply(df, 1, function(x)x[maxn(2)(x)])
    [1] 0 3 1 1 0
    

    Updated

    Why using closure here?

    One reason is that you can define a function such as:

    max2 <- maxn(2)
    max3 <- maxn(3)
    

    then, use it

    > apply(df, 1, max2)
    [1] 2 3 1 2 1
    > apply(df, 1, max3)
    [1] 3 2 2 3 2
    

    I’m not sure if the advantage is obvious, but I like this way, since this is more functional-ish way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some C code which is portable only so far as
I'm trying to write some Haskell code in which there are multiple data types,
I am trying to write some code that that will draw the line which
I'm trying to write some C++ code for Windows 7 which will connect to
I'm trying to write some code which will highlight any text selected on a
I am trying to write some code which retrieves the first 10 words of
I want to write some code which takes any positive, even number (greater than
I am currently trying to write some code which is going to send a
I'm trying to write some code which uses pipes to communicate between a parent
I'm trying to write some python code which can create multipart mime http requests

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.