0

Let the example explain (just a toy example to demo the problem I met in a piece of complicated code). In the following example, why data.frame a is not changed after sapply?

> a=data.frame(A=c(1,2,3),B=c(4,5,6))
> a
  A B
1 1 4
2 2 5
3 3 6
> a[c(T,T,F),]
  A B
1 1 4
2 2 5
> sapply(c(1,2), function(x) a=a[c(T,T,F),])
  [,1]      [,2]
A Numeric,2 Numeric,2
B Numeric,2 Numeric,2
> a
  A B
1 1 4
2 2 5
3 3 6

A second thought, this is likely because of the namespace. The changed a only exist inside the function. My question is then how to make this global?

11
  • In general, R does not modify objects in place. It is, after all, functional in nature. But beyond that, I'm not sure what you're trying to accomplish with that sapply call. What exactly is your expected output? Maybe you simply meant to assign the result of sapply to a? Commented Mar 1, 2014 at 0:25
  • just a stupid example to explain my problem straightforward. Commented Mar 1, 2014 at 0:28
  • 1
    Well, in R you typically would not modify objects in place. You could use <<-, but that is considered very bad practice. Instead, you would simply so a <- sapply(...). Commented Mar 1, 2014 at 0:29
  • 3
    I mean that, like Las Vegas, "what happens inside a function stays inside a function", and that is a fundamental design principle of R. Violating it is considered bad practice. But if you really have to (and you shouldn't) you could use <<- or assign. Commented Mar 1, 2014 at 0:32
  • 1
    ...if you are just looking for a reference on scope in R, you could simply browse the manual. Commented Mar 1, 2014 at 0:38

1 Answer 1

3

Here is a simpler example:

f <- function() a <- 1
a <- 2
f()
a # a is still 2

What is happening is:

  1. The function f and variable a are created in the global environment.
  2. f is run with no arguments.
  3. Inside the running f a new variable a is created with the value 1.
    The a it creates in the function is unrelated to the a in the global environment.
  4. f finishes running and returns the result 1 and destroys the a that was created in the function.
  5. We ask to display a and it has been unchanged so it displays 2.

If we assign to a variable name in a function R creates a new object in the function with that name. It will not change a variable of the same name outside of the function but rather now there exist two variables with the same name until the function finishes running and then the variables created in the function are destroyed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.