1

I have the following function:

s_c <- function(n, t){ r_num <- runif(1,min=0,max=1) use <- sample(s[,1],1) use2 <- subset(s,s[,1]==use,2) use2 <- as.numeric(use2) ne_s <- sample(subset(s,s[,2]!=use2,2),1) Return(use) if (t>50 & r_num<0.5){ ne_s } else 0 } 

I would actually like to use the variable created in the function in a command outside the function, so I would like to "return" in the sense of being able to refer to the variable outside the function

Question 2:

What if I would like to do an assignment within the if statement, for example

 if (t>50 & r_num<0.5){ s[,4]=use } 

Can this be done?

4
  • Do you want to return two values? Commented Dec 19, 2012 at 14:23
  • I would actually like to use the variable created in the function in a command outside the function, so I would like to "return" in the sense of being able to refer to the variable outside the function Commented Dec 19, 2012 at 14:24
  • Do you mean that you want this function to modify a local variable in another function? Commented Dec 19, 2012 at 14:54
  • well not necessarily in another function but within the program outside the current function Commented Dec 19, 2012 at 15:00

2 Answers 2

4

The "list" solution as mentioned by Matthew:

.....

list(variable1 = VarInFunction, variable2 = VarInFunction2) 

Like this, when you run your function, you can store its results.

Result <- s_c(10,1) 

You can then ask the results specifically like:

Result$variable1 Result$variable2 

Note that VarInFunction is the variable as it is called within the function, and is renamed to variable1 (or whatever you want), and is stored in Result.

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

1 Comment

And the very important difference here is that you run zero risk of fouling up some other variable in your environment. I understand that you want to update values in s , but once you do so, you can never get the original values back. As an alternative way of updating a variable outside the function, consider a script like s_temp <- s_c(n,t); s.old<-s; s<-s_temp .
3

Although you should probably return the two values as a list, you can affect the parent's environment quite easily.

Replace this:

Return(use) 

with this:

use <<- use 

For your edit, you could use the syntax:

s[,4] <<- use 

Note: The "parent's environment" is NOT the environment in which the function is called. It is the environment in which the function is defined. It is for this reason that a function in R is known as a closure. Explicitly:

s_c <- function() { use <<- 1 } f <- function() { use <- 0 s_c() return(use) } 

f() will return 0. s_c() is modifying a different use variable (in a different environment).

3 Comments

Which is generally not a good idea todo, gobal variables. It creates tightly coupled code, which negates the whole idea of using a function.
and what if I would like to do an assignment within the IF statement, for example if some condition holds, I would like to assign an outside variable a new value
You can. The question is whether you should. For example, if you call this function from another function, unexpected things may happen (depending on what you expect, of course).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.