1

I trying to use this function

chan <- function(x) {
  for (i in x)
  {assign(i,sample(1:10,2))} 
  out <- data.frame(sapply(x,get))
  out
}

which x will be a list of string names, this function will assign 2 random numbers to each variable names and return it out as a data frame.

for example

 x <- c("e","f")

When I use this function, it gives error Error in FUN(..., ) : object ... not found

but if I don't use the function, just run the loop, it works. such as:

x <- c("e","f")
for (i in x)
 {assign(i,sample(1:10,1))}
 out <- data.frame(sapply(x,get))

I wonder what's wrong here.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
Diana Wong
  • 11
  • 1
  • You didn't post the actual the error is, but it has nothing particular to do with `sapply`, it would error with any function call. Your issue is all about trying to read or assign variables which are not local variables, but exist in the parent environment. – smci Jul 07 '15 at 16:41
  • This is a duplicate. See [Confusion about reference question and tags for R questions on functions accessing/assigning to parent environment variables](http://meta.stackoverflow.com/questions/298735/confusion-about-reference-question-and-tags-for-r-questions-on-functions-accessi) – smci Jul 07 '15 at 17:28
  • @smci: Don't state it is a dupe and don't vote. If this is a duplicate, vote to close as such. – Martijn Pieters Jul 13 '15 at 07:40
  • @MartijnPieters: see [Confusion about reference question and tags for R questions on functions accessing/assigning to parent environment variables](http://meta.stackoverflow.com/questions/298735/confusion-about-reference-question-and-tags-for-r-questions-on-functions-accessi). This topic in R is problematic because there are many duplicates, however they are inconsistently tagged and titled. I actually spent time researching and documenting that. If we merely pick a terrible primary reference to close this in favor of, we're only kicking things down the road. Please respond in that question. – smci Jul 13 '15 at 13:04
  • @smci: What I mean is that you shouldn't clutter up comments with 'duplicate' claims. Either vote to close, or don't. If you don't have a good duplicate target, consider creating one; see [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) for a good example from the Python community. – Martijn Pieters Jul 13 '15 at 13:06
  • As already covered in that question, I'm not able to write a great reference answer, that's why I'm suggesting someone from the R community to write one; but before writing it, R users first need to standardize on terms and topics ('environment' or 'scope' etc?), and make titles consistent. I don't understand why taking time to document what needs fixing is getting so many unconstructive comments. I don't appreciate the downvotes either. – smci Jul 13 '15 at 13:35

1 Answers1

1

You can tell assign and get what environments to use when assigning/getting variables using pos (x will be in your global environment after this)

chan <- function(x) {
    for (i in x) {assign(i,sample(1:10,2), pos=1)}
    out <- data.frame(sapply(x, get, pos=1))
    out
}

or just assign in the local function environment

chan <- function(x) {
    e <- environment()
    for (i in x) {
        assign(i,sample(1:10,2), pos=e)
    }
    out <- data.frame(sapply(x, get, pos=e))
    out
}
Rorschach
  • 31,301
  • 5
  • 78
  • 129