Hi! So I'm trying as the header suggests to assign the value(s) output by a function to a variable, say 'y' Problem is from what I gather any variables introduced within the function are contained and the only output I can get is "return(value)" which is awkward to work with. Any suggestions? Cheers! -- View this message in context: http://r.789695.n4.nabble.com/assign-variables-to-function-output-tp4705920.html Sent from the R help mailing list archive at Nabble.com.
Collect in a vector or dataframe or list the variables of interest and output it. Il 16/apr/2015 10:57, "merm" <pionescu at student.unimelb.edu.au> ha scritto:> Hi! > > So I'm trying as the header suggests to assign the value(s) output by a > function to a variable, say 'y' > > Problem is from what I gather any variables introduced within the function > are contained and the only output I can get is "return(value)" which is > awkward to work with. Any suggestions? > > Cheers! > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/assign-variables-to-function-output-tp4705920.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi merm,
In case Sergio's message is a little cryptic:
return_a_list<-function() {
a<-"First item of list"
b<-c(2,4,6,8)
c<-matrix(1:9,nrow=3)
return(list(a,b,c))
}
x<-return_a_list()
x
Jim
On Thu, Apr 16, 2015 at 7:21 PM, Sergio Fonda <sergio.fonda99 at
gmail.com> wrote:> Collect in a vector or dataframe or list the variables of interest and
> output it.
> Il 16/apr/2015 10:57, "merm" <pionescu at
student.unimelb.edu.au> ha scritto:
>
>> Hi!
>>
>> So I'm trying as the header suggests to assign the value(s) output
by a
>> function to a variable, say 'y'
>>
>> Problem is from what I gather any variables introduced within the
function
>> are contained and the only output I can get is
"return(value)" which is
>> awkward to work with. Any suggestions?
>>
>> Cheers!
>>
>>
>>
>> --
>> View this message in context:
>>
http://r.789695.n4.nabble.com/assign-variables-to-function-output-tp4705920.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
I don't disagree with the other answers, but I'd like to talk about what
I
see as a more underlying issue.
Generally speaking, functions return the value of the last expression
within them.
myfun <- function(x) {
z <- sqrt(x)
2
}
Then
y <- myfun(7)
assigns 2 to y because 2 was the last expression in the function.
myfun <- function(z) {
cat('inside myfun\n')
y <- z-7
x <- 2*z
sqrt(z)
}
y <- myfun(c(1,4,9))
assigns c(1,2,3) to y because sqrt(z) was the last expression. x and y are
lost and gone forever.
Note that return() was not used, and does not need to be used.
Thus, if you want to return something that includes some of the variables
introduced inside the function, you gather them into a suitable structure
and make that the last expression in the function.
myfun <- function(z) {
y <- z-7
x <- 2*z
list(x=x, y=y, rtz=sqrt(z))
}
Then y <- myfun(4) returns a list with three elements whose values are -3,
8, and 2.
Again no use of return(). There are cases where return() is useful, but it
is not needed in simple cases like these.
(and I can't imagine what's awkward about using return(value), but
that's
another question)
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 4/15/15, 3:17 PM, "merm" <pionescu at student.unimelb.edu.au>
wrote:
>Hi!
>
>So I'm trying as the header suggests to assign the value(s) output by a
>function to a variable, say 'y'
>
>Problem is from what I gather any variables introduced within the function
>are contained and the only output I can get is "return(value)"
which is
>awkward to work with. Any suggestions?
>
>Cheers!
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/assign-variables-to-function-output-tp470592
>0.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.