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?

RNA
  • 146,987
  • 15
  • 52
  • 70
  • 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`? – joran Mar 01 '14 at 00:25
  • just a stupid example to explain my problem straightforward. – RNA Mar 01 '14 at 00:28
  • What exactly are you trying to do? – Rich Scriven Mar 01 '14 at 00: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(...)`. – joran Mar 01 '14 at 00:29
  • what do you mean by modify in place? you can perfectly do `a=a[c(T,T,F),]` – RNA Mar 01 '14 at 00:31
  • 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`. – joran Mar 01 '14 at 00:32
  • Is this what you want: `sapply(a[,1:2], function(x) x[c(T,T,F)])` ? – Rich Scriven Mar 01 '14 at 00:33
  • @RScriv, this is just an example to show the problem I have, not what I want. – RNA Mar 01 '14 at 00:36
  • 1
    ...if you are just looking for a reference on scope in R, you could simply browse the [manual](http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Scope). – joran Mar 01 '14 at 00:38
  • yes. This is badly asked question. This scope thing is also common in programming. I guess the eccentricity of R just made me treat it as informal language. – RNA Mar 01 '14 at 02:13
  • Further reading on R scope and globals: http://stackoverflow.com/questions/5526322/examples-of-the-perils-of-globals-in-r-and-stata http://stackoverflow.com/q/9851655/636656 http://stackoverflow.com/q/14166207/636656 – Ari B. Friedman Mar 01 '14 at 02:38

1 Answers1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341