Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.
I would like to know the best way to export several doubles from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponentioal<-exp(x)
factorialVal<-factorial(x)
}
Thanks again for all your help.
Best way to export values from a function?
13 messages · Jason Rupert, Daniel Nordlund, jim holtman +6 more
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Jason Rupert
Sent: Wednesday, July 08, 2009 5:35 PM
To: R-help at r-project.org
Subject: [R] Best way to export values from a function?
Maybe there is a great website out there or white paper that
discusses this but again my Google skills (or lack there of)
let me down.
I would like to know the best way to export several doubles
from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponentioal<-exp(x)
factorialVal<-factorial(x)
}
I can't vouch for the best way, but here is one way, return a list of the
values
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponential<-exp(x)
factorialVal<-factorial(x)
return(list(squared=squared, cubed=cubed, exponential=exponential,
factorialVal=factorialVal))
}
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA USA
You can return a list, vector, or any other object: The last value is the return value unless you do an explicit return()
On Wed, Jul 8, 2009 at 8:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.
I would like to know the best way to export several doubles from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
? ? ? ?squared<-x^2
? ? ? ?cubed<-x^3
? ? ? ?exponentioal<-exp(x)
? ? ? ?factorialVal<-factorial(x)
}
Thanks again for all your help.
______________________________________________ R-help at r-project.org mailing list 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.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Wed, Jul 8, 2009 at 5:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.
I would like to know the best way to export several doubles from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
? ? ? ?squared<-x^2
? ? ? ?cubed<-x^3
? ? ? ?exponentioal<-exp(x)
? ? ? ?factorialVal<-factorial(x)
}
Thanks again for all your help.
______________________________________________ R-help at r-project.org mailing list 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'm a newbie so mine;s probably not the best but it seems to work:
multipleoutput<-function(x)
{
answer<- c(0,0,0,0)
MyFuncNames <- c("Squared","Cubed","Exp","Fac")
answer$squared<-x^2
answer$cubed<-x^3
answer$exponential<-exp(x)
answer$factorial<-factorial(x)
return(answer)
}
X = c(0,0,0,0)
X
mode(X)
names(X)
MyNames = c("Squared","Cubed","Exp","Fac")
MyNames
names(X) = MyNames
X <- multipleoutput(2)
X
class(X)
dim(X)
On Wed, Jul 8, 2009 at 8:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.
Yeah, R is difficult to search for - I've had partial success with rseek.org, though.
I would like to know the best way to export several doubles from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
? ? ? ?squared<-x^2
? ? ? ?cubed<-x^3
? ? ? ?exponentioal<-exp(x)
? ? ? ?factorialVal<-factorial(x)
}
You can always do:
multipleoutput <- function (x) { return (c(square = x^2, cube = x^3, exp = exp(x))) }
But then you'd have to call it like so:
mapply(multipleoutput, c(0,1,2))
[,1] [,2] [,3] square 0 1.000000 4.000000 cube 0 1.000000 8.000000 exp 1 2.718282 7.389056 If you call it like so:
multipleoutput(c(0,1,2))
square1 square2 square3 cube1 cube2 cube3 exp1 exp2
0.000000 1.000000 4.000000 0.000000 1.000000 8.000000 1.000000 2.718282
exp3
7.389056
then R flattens the result. Weird.
- Godmar
On Wed, Jul 8, 2009 at 5:55 PM, Mark Knecht<markknecht at gmail.com> wrote:
On Wed, Jul 8, 2009 at 5:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
Maybe there is a great website out there or white paper that discusses this but again my Google skills (or lack there of) let me down.
I would like to know the best way to export several doubles from a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
? ? ? ?squared<-x^2
? ? ? ?cubed<-x^3
? ? ? ?exponentioal<-exp(x)
? ? ? ?factorialVal<-factorial(x)
}
Thanks again for all your help.
<SNIP>
This version runs a bit better than my last and I find it a bit more
readable, but there's a warning for whatever the first computation is
inside the function that I'd like to understand.
- Mark
multipleoutput <- function(x) {
answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
answer$Squ=x^2
answer$Cub<-x^3
answer$Exp<-exp(x)
answer$Fac<-factorial(x)
return(answer)
}
X = data.frame("Squared"=0,"Cubed"=0,"Exp"=0,"Fac"=0)
X
mode(X)
names(X)
X[1,] <- multipleoutput(2)
X
class(X)
<PRODUCES>
multipleoutput <- function(x) {
+ answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
+ answer$Squ=x^2
+ answer$Cub<-x^3
+ answer$Exp<-exp(x)
+ answer$Fac<-factorial(x)
+ return(answer)
+ }
X = data.frame("Squared"=0,"Cubed"=0,"Exp"=0,"Fac"=0)
X
Squared Cubed Exp Fac 1 0 0 0 0
mode(X)
[1] "list"
names(X)
[1] "Squared" "Cubed" "Exp" "Fac"
X[1,] <- multipleoutput(2)
Warning message: In answer$Squ = x^2 : Coercing LHS to a list
X
Squared Cubed Exp Fac 1 4 8 7.389056 2
class(X)
[1] "data.frame"
Mark and Jason,
At 7:23 PM -0700 7/8/09, Mark Knecht wrote:
On Wed, Jul 8, 2009 at 5:55 PM, Mark Knecht<markknecht at gmail.com> wrote:
On Wed, Jul 8, 2009 at 5:34 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:
Maybe there is a great website out there or white paper that
discusses this but again my Google skills (or lack there of) let
me down.
I would like to know the best way to export several doubles from
a function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponentioal<-exp(x)
factorialVal<-factorial(x)
}
Thanks again for all your help.
<SNIP>
This version runs a bit better than my last and I find it a bit more
readable, but there's a warning for whatever the first computation is
inside the function that I'd like to understand.
- Mark
multipleoutput <- function(x) {
answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
answer$Squ=x^2
answer$Cub<-x^3
answer$Exp<-exp(x)
answer$Fac<-factorial(x)
return(answer)
}
X = data.frame("Squared"=0,"Cubed"=0,"Exp"=0,"Fac"=0)
X
mode(X)
names(X)
X[1,] <- multipleoutput(2)
X
class(X)
<PRODUCES>
multipleoutput <- function(x) {
+ answer = c("Squ"=0,"Cub"=0,"Exp"=0,"Fac"=0)
+ answer$Squ=x^2
+ answer$Cub<-x^3
+ answer$Exp<-exp(x)
+ answer$Fac<-factorial(x)
+ return(answer)
+ }
X = data.frame("Squared"=0,"Cubed"=0,"Exp"=0,"Fac"=0)
X
Squared Cubed Exp Fac 1 0 0 0 0
mode(X)
[1] "list"
names(X)
[1] "Squared" "Cubed" "Exp" "Fac"
X[1,] <- multipleoutput(2)
Warning message: In answer$Squ = x^2 : Coercing LHS to a list
X
Squared Cubed Exp Fac 1 4 8 7.389056 2
class(X)
[1] "data.frame"
>
?list
e.g.,
multipleoutput <- function(x) {
answer = list()
answer$Squ=x^2
answer$Cub<-x^3
answer$Exp<-exp(x)
answer$Fac<-factorial(x)
return(answer)
}
Bill
William Revelle http://personality-project.org/revelle.html Professor http://personality-project.org/personality.html Department of Psychology http://www.wcas.northwestern.edu/psych/ Northwestern University http://www.northwestern.edu/ Attend ISSID/ARP:2009 http://issid.org/issid.2009/
Hi,
On Jul 8, 2009, at 8:34 PM, Jason Rupert wrote:
Maybe there is a great website out there or white paper that
discusses this but again my Google skills (or lack there of) let me
down.
I would like to know the best way to export several doubles from a
function, where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponentioal<-exp(x)
factorialVal<-factorial(x)
}
There already have been some suggestions on how to do this the "normal" R way, so let's go ahead and use the "return a list" method (I think it's better than using the `c(squared=x^2, cubed=...)`). Here's an interesting way to receive the assignments. Check out this function: http://code.google.com/p/miscell/source/browse/rvalues/rvalues.r With that ':=' function loaded, you could do this: ============ multipleout <- function(x) { list(squared=x^2, cubed=x^3, exponential=exp(x), factorial=factorial(x)) } c(sq,cu,ex,fa) := multipleout(1:3) show(sq) [1] 1 4 9 show(cu) [1] 1 8 27 show(ex) [1] 2.718282 7.389056 20.085537 show(fa) [1] 1 2 6 ============= [I can't remember how I stumbled onto this code for the ':=' function (I think it was from a thread on the BioC list about package updates)] I'm not saying that you *should* do it this way, but it's kind of cool that you could ... -steve -- Steve Lianoglou Graduate Student: Physiology, Biophysics and Systems Biology Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090709/1eb2a0d2/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090709/dcb1298c/attachment-0001.pl>
Hi r-help-bounces at r-project.org napsal dne 09.07.2009 02:57:33:
On Wed, Jul 8, 2009 at 8:34 PM, Jason Rupert<jasonkrupert at yahoo.com>
wrote:
Maybe there is a great website out there or white paper that discusses
this
but again my Google skills (or lack there of) let me down. Yeah, R is difficult to search for - I've had partial success with rseek.org, though.
I would like to know the best way to export several doubles from a
function,
where the doubles are not an array.
Here is a contrived function similar to my needs:
multipleoutput<-function(x)
{
squared<-x^2
cubed<-x^3
exponentioal<-exp(x)
factorialVal<-factorial(x)
}
You can always do:
multipleoutput <- function (x) { return (c(square = x^2, cube = x^3,
exp = exp(x))) }
But then you'd have to call it like so:
mapply(multipleoutput, c(0,1,2))
[,1] [,2] [,3] square 0 1.000000 4.000000 cube 0 1.000000 8.000000 exp 1 2.718282 7.389056 If you call it like so:
multipleoutput(c(0,1,2))
square1 square2 square3 cube1 cube2 cube3 exp1 exp2
0.000000 1.000000 4.000000 0.000000 1.000000 8.000000 1.000000 2.718282
exp3
7.389056
then R flattens the result. Weird.
Not so weird. What do you expect from
c(1:5, 10:20, 30:50)
That is basically what your function do. With slight modification you can
get tabular output without mapply
multipleoutput <- function (x) {
result.s <- x^2
result.c <- x^3
result.e <- exp(x)
cbind(square=result.s, cube=result.c, exp=result.e)
}
If the output could be mixed type }numeric, character, ...) use data.frame
instead of cbind
Regards
Petr
- Godmar
______________________________________________ R-help at r-project.org mailing list 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090709/299814ae/attachment-0001.pl>
Hi Godmar Back <godmar at gmail.com> napsal dne 09.07.2009 14:09:42:
On Thu, Jul 9, 2009 at 4:50 AM, Petr PIKAL <petr.pikal at precheza.cz>
wrote:
Hi r-help-bounces at r-project.org napsal dne 09.07.2009 02:57:33:
<snip>
Not so weird. What do you expect from c(1:5, 10:20, 30:50) You mean what I expect personally, with my background? I'd expect a jagged array of arrays. 1 2 3 4 5 10 11 12 .. 20 30 31 .. 50 If operator c() constructs an array out of its arguments, and if the
sequence
operator : constructs an array, then c(1:5) should construct an array of
arrays.
is.array(1:5)
[1] FALSE
is.vector(1:5)
[1] TRUE
Why. Not much is said about arrays in man page for c. The operator concatenates its arguments. If they are vectors the output is again a vector. If they are lists the output is list. try c(1:5,10:20) c(1:5, list(10:20) c(1:5, data.frame(10:20) and regarding arrays, matrices and vectors - AFAIK arrays and matrices are just vectors with dim attribute, which is stripped by c c is sometimes used for its side effect of removing attributes except names, for example to turn an array into a vector. as.vector is a more intuitive way to do this, but also drops names. Note too that methods other than the default are not required to do this (and they will almost certainly preserve a class attribute).
That is basically what your function do. With slight modification you
can
get tabular output without mapply
multipleoutput <- function (x) {
result.s <- x^2
result.c <- x^3
result.e <- exp(x)
cbind(square=result.s, cube=result.c, exp=result.e)
}
Just for clarification: is there any significance to your using the . in
the
names result.s, result.c, etc. like there would be in some other
languages
where dot is an operator?
No. AFAIK one dot does not have any special meaning unless you use it in the beginning of name. In that case the variable is not listed by ls() function. See ?ls and argument all.names Regards Petr
- Godmar