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

  • Home
  • SEARCH
  • 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 6602491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:52:59+00:00 2026-05-25T18:52:59+00:00

I have background of Java and Python and I’m learning R recently. Today I

  • 0

I have background of Java and Python and I’m learning R recently.

Today I found that R seems to handle objects quite differently from Java and Python.

For example, the following code:

x <- c(1:10)
print(x)
sapply(1:10,function(i){
            x[i] = 4
        })
print(x)

The code gives the following result:

[1]  1  2  3  4  5  6  7  8  9 10
[1]  1  2  3  4  5  6  7  8  9 10

But I expect the second line of output to be all ‘4’ since I modified the vector in the sapply function.

So does this mean that R make copies of objects in function call instead of reference to the objects?

  • 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-25T18:53:00+00:00Added an answer on May 25, 2026 at 6:53 pm

    x is defined in the global environment, not in your function.

    If you try to modify a non-local object such as x in a function then R makes a copy of the object and modifies the copy so each time you run your anonymous function a copy of x is made and its ith component is set to 4. When the function exits the copy that was made disappears forever. The original x is not modified.

    If we were to write x[i] <<- i or if we were to write x[i] <- 4; assign("x", x, .GlobalEnv) then R would write it back. Another way to write it back would be to set e, say, to the environment that x is stored in and do this:

    e <- environment()
    sapply(1:10, function(i) e$x[i] <- 4)
    

    or possibly this:

    sapply(1:10, function(i, e) e$x[i] <- 4, e = environment())
    

    Normally one does not write such code in R. Rather one produces the result as the output of the function like this:

    x <- sapply(1:10, function(i) 4)
    

    (Actually in this case one could write x[] <- 4.)

    ADDED:

    Using the proto package one could do this where method f sets the ith component of the x property to 4.

    library(proto)
    
    p <- proto(x = 1:10, f = function(., i) .$x[i] <- 4)
    
    for(i in seq_along(p$x)) p$f(i)
    p$x
    

    ADDED:

    Added above another option in which we explicitly pass the environment that x is stored in.

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

Sidebar

Related Questions

I have a background in java, and I'm learning python. I'll need to be
I recently got into Java. I have a background in dynamic languages and I'm
I have been learning Python by following some pygame tutorials . Therein I found
Hi I have background in Java and currently learning C++. In Java it is
I have a strong python background and a total newbie with Java, so I
I come from a Java background and have been using C# for the last
Static variable gotcha in php I am from Java background and have switched to
I have a background of programming in Java and C++, so I know all
I come from Java Background and so used to Debugging using Eclipse but have
I'm relative new to C++ and my background is in Java. I have to

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.