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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:28:25+00:00 2026-05-23T03:28:25+00:00

I have a data frame, and I want to do some calculation with existing

  • 0

I have a data frame, and I want to do some calculation with existing columns and create new column in my data set which is a combination of existing… I can do this easily outside function… but if I wrap the code witin function, the changes I made (inside functions) are not visible outside function… i.e. the new column doesn’t exist…

I would appreciate sample code 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-05-23T03:28:26+00:00Added an answer on May 23, 2026 at 3:28 am

    I’ll assume it is about R… R does not pass arguments by reference (environments and reference classes (S5) are an exception, but this is out of the current range of abstraction). Thus, when you write

    addThree<-function(x){
     x<-x+3
    }
    4->y
    addThree(y)
    

    y is still 4 at the end of code, because inside the function, x is the fresh copy of ys value, not the y itself (again, not exactly, but those are higher order details).

    Thus, you must adapt to R’s pass-by-copy scheme and return the altered value and assign it back to your variable (using old wording, there are no procedures in R):

    addThree<-function(x){
     return(x+3)
    }
    4->y
    addThree(y)->y
    #y is now 7
    

    Don’t worry, this works smoothly for even more complex objects because R is garbage-collected and has lazy evaluation.

    BTW, you can omit return if you want to return the last value produced in function, i.e. addThree‘s definition may look like this:

    addThree<-function(x) x+3
    
    • 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 two columns. First column contains categories such as
I have a data frame where one particular column has a set of specific
I have a data frame with two columns: an ID and some numeric value
I want to rename some random columns of a large data frame and I
I'm trying to normalize some data which I have in a data frame. I
I have a data.frame which has multiple columns. One of the columns is time
I have data frame with some numerical variables and some categorical factor variables. The
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data frame in R that has come about from running some

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.