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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:50:50+00:00 2026-06-06T05:50:50+00:00

I have a two column dataframe in R and i want to add a

  • 0

I have a two column dataframe in R and i want to add a third column with values that are a function of the first two.
As long as the function is a simple mathematical formula no problem
data$c <- data$a*15 - 10 + data$b*0.5
But my function is non continous and contains conditionals, i would like to do:
foo <- function(x,y){if (x<2) {return(0)} else {....}
and
data$c <- foo(data$a, data$b)
But this gets me errors. What am i missing?

  • 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-06T05:50:53+00:00Added an answer on June 6, 2026 at 5:50 am
    df = data.frame(a=rnorm(20),b=runif(20))
    
    new_var = function(x,y)
    {
      if(x<0)
      { x = x^2}
      else
      { x = x^3}
    
      if(y<.5)
      { -log10(y)}
      else
      {
        exp(y)
      }
      return((2*(x+y))/x*y)
    }
    
    df$c = sapply(1:length(df$a), function(i){new_var(df$a[i],df$b[i])})
    
    head(df)
                a          b          c
    1 -0.09149107 0.04834675 0.65517298
    2 -2.43732351 0.04086087 0.08228385
    3 -1.69573283 0.54795413 1.30474339
    4 -0.40106220 0.72952356 8.07641367
    5 -0.38244706 0.65524147 7.18118555
    6  0.76334945 0.38455655 1.43404971
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe in R that I want to randomize, keeping the first
I have a state column in a dataframe and I want to create two
I have a data frame with two columns. First column contains categories such as
I have two tables: object that has object_id column and avalues that have object_id
We have two tables in our application that both have a ShowOrder column. We
I have two or more tables which hold values under the same column name.
I have two CSV files which use @ to divide each column. The first
I have two data frames, Data1 and Data2, that I want to merge based
I have two dataframes and I wish to insert the values of one dataframe
I have a dataframe called prices_df to which I add an extra column and

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.