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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:26:32+00:00 2026-06-12T09:26:32+00:00

I apologize if the title may not be clear. With df <- data.frame(profit =

  • 0

I apologize if the title may not be clear.

With

df <- data.frame(profit = c(1, 1, 0, 0, 0, -1),
                 offerA = round(rnorm(6), 2), 
                 offerB = round(runif(6), 2), 
                 offerC = sample(1:6))

df
  profit offerA offerB offerC
1      1  -0.51   0.91      6
2      1  -0.03   0.75      4
3      0  -1.02   0.28      5
4      0   0.63   0.61      1
5      0   2.32   0.37      2
6     -1  -0.15   0.43      3

I need to add a field named bid depending on the value of profit under the following conditions:

with(df,
  if (profit > 0) {
    apply(cbind(offerA, offerB, offerC), 1, max)
  }
  else if (profit = 0) {
    apply(cbind(offerA, offerB, offerC), 1, mean)
  }
  else if (profit < 0) {
    apply(cbind(offerA, offerB, offerC), 1, min)
  }
)

In this example the new df will be:

df
  profit offerA offerB offerC   bid
1      1  -0.51   0.91      6     6
2      1  -0.03   0.75      4     4
3      0  -1.02   0.28      5  1.42
4      0   0.63   0.61      1  0.75
5      0   2.32   0.37      2  1.56
6     -1  -0.15   0.43      3 -0.15

Because the value of bid is computed by rows, I want to write a function addBid() so that I can use something like apply(df, 1, addBid) but I can’t think of a good way adding my conditions above into the function. I hope I’m clear with my question. Thanks!

  • 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-12T09:26:33+00:00Added an answer on June 12, 2026 at 9:26 am

    Maybe this could be useful

    bid <- function(x, ref, add=FALSE){
      ref <- match(ref,names(df))
      result <- ifelse(df[,ref]>0, apply(df[,-ref],1,max), 
             ifelse(df[,ref]==0, rowMeans(df[,-ref]),  apply(df[,-ref],1,min)) )
      if(add){
        result <- cbind(x, bid=result)
      }
      return(result)
    }
    

    Where x is your data.frame, ref is the reference variable (profit) and add is a logical indicating if you want bid as a vector (if add=FALSE) or your original data.frame with bid as a new column (if add=TRUE).

    set.seed(001)
    df <- data.frame(profit = c(1, 1, 0, 0, 0, -1),
                     offerA = round(rnorm(6), 2), 
                     offerB = round(runif(6), 2), 
                     offerC = sample(1:6))
    
    > bid(df, ref='profit')  # returns a vector
    [1]  3.000000  4.000000  1.643333  1.033333  1.016667 -0.820000
    > bid(df, ref='profit', add=TRUE) # returns a data.frame = df + bid
      profit offerA offerB offerC       bid
    1      1  -0.63   0.69      3  3.000000
    2      1   0.18   0.38      4  4.000000
    3      0  -0.84   0.77      5  1.643333
    4      0   1.60   0.50      1  1.033333
    5      0   0.33   0.72      2  1.016667
    6     -1  -0.82   0.99      6 -0.820000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In advance, I apologize if the question title may not be be convenient. I'm
I apologize that the title may not make sense, so let me describe my
I know the title might not be clear and I apologize about that. so
I apologize if the title is not clear, but I couldn't explain it succinctly.
First of all, I have to apologize for not having a better title. Feel
Before I start, I apologize for a bad title but I could not come
Ok, the title may not be the most descriptive. It's easier to explain with
The title may not be very appropriate, so apologies in advance. I'm building a
I realize that the title may be somewhat confusing, so I apologize. Basically, this
[I apologize for the inept title; I could not come up with anything better.

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.