[R] Re use objects from within a custom made function
Hi everyone, i'm having a problem extracting objects out of functions i've created, so i can use them for further analysis. Here's a small example: # --- test - function(i, j){ x - i:j y - i*j z - i/j return(x,y,z) } # --- This returns the 3 objects as $x, $y and $z. I cannot, however, access these objects individually by typing for example: test$x I know i can do this by adding an extra arrow head to the assignment arrow (-), but I am sure that is not how it is done in some of the established R functions (like when calling lm$coef out of the lm function). Is there a simple command i've omitted from the function that allows access to objects inside it? Thanks in advance, Steve -- View this message in context: http://www.nabble.com/Reuse-objects-from-within-a-custom-made-function-tp25864695p25864695.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Re use objects from within a custom made function
test$x doesn't evaluate the function, you want something like test(1,2)$x, e.g.: test - function(i, j){ x - i:j y - i*j z - i/j return(list(x=x,y=y,z=z)) } test(1,2)$x [1] 1 2 test(1,2)$y [1] 2 test(1,2)$z [1] 0.5 Or if you want to avoid evaluating your function multiple times: res - test(1,2) res$x [1] 1 2 res$y [1] 2 res$z [1] 0.5 res $x [1] 1 2 $y [1] 2 $z [1] 0.5 Stropharia wrote: Hi everyone, i'm having a problem extracting objects out of functions i've created, so i can use them for further analysis. Here's a small example: # --- test - function(i, j){ x - i:j y - i*j z - i/j return(x,y,z) } # --- This returns the 3 objects as $x, $y and $z. I cannot, however, access these objects individually by typing for example: test$x I know i can do this by adding an extra arrow head to the assignment arrow (-), but I am sure that is not how it is done in some of the established R functions (like when calling lm$coef out of the lm function). Is there a simple command i've omitted from the function that allows access to objects inside it? Thanks in advance, Steve __ R-help@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.
Re: [R] Re use objects from within a custom made function
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Stropharia Sent: Monday, October 12, 2009 4:07 PM To: r-help@r-project.org Subject: [R] Re use objects from within a custom made function Hi everyone, i'm having a problem extracting objects out of functions i've created, so i can use them for further analysis. Here's a small example: # --- test - function(i, j){ x - i:j y - i*j z - i/j return(x,y,z) } # --- This returns the 3 objects as $x, $y and $z. I cannot, however, access these objects individually by typing for example: test$x I know i can do this by adding an extra arrow head to the assignment arrow (-), but I am sure that is not how it is done in some of the established R functions (like when calling lm$coef out of the lm function). Is there a simple command i've omitted from the function that allows access to objects inside it? Thanks in advance, Steve -- Steve, I see a couple of problems (at least from my perspective). If you ever got your function to run, you would see a warning stating that returning multiple objects is deprecated. In your example of using the function, you don't call it with any parameters, and the definition has no default values. I also would be inclined to assign the results of the function call to a variable/object and access the values from there. So I would approach your problem as follows: test - function(i, j){ x - i:j y - i*j z - i/j list(x=x, y=y, z=z) } my.result - test(1,4) my.result$x [1] 1 2 3 4 If you don't want to assign the output of test() to an object, then you will need to do something like test(1,4)$x But this is inefficient because each time you access one of the components, you need to rerun the function. Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA __ R-help@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.
Re: [R] Re use objects from within a custom made function
Thanks a lot Tony and Daniel for making that clear. best, Steve Stropharia wrote: Hi everyone, i'm having a problem extracting objects out of functions i've created, so i can use them for further analysis. Here's a small example: # --- test - function(i, j){ x - i:j y - i*j z - i/j return(x,y,z) } # --- This returns the 3 objects as $x, $y and $z. I cannot, however, access these objects individually by typing for example: test$x I know i can do this by adding an extra arrow head to the assignment arrow (-), but I am sure that is not how it is done in some of the established R functions (like when calling lm$coef out of the lm function). Is there a simple command i've omitted from the function that allows access to objects inside it? Thanks in advance, Steve -- View this message in context: http://www.nabble.com/Reuse-objects-from-within-a-custom-made-function-tp25864695p25866081.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Re use objects from within a custom made function
On Oct 12, 2009, at 7:06 PM, Stropharia wrote: Hi everyone, i'm having a problem extracting objects out of functions i've created, so i can use them for further analysis. Here's a small example: # --- test - function(i, j){ x - i:j y - i*j z - i/j return(x,y,z) } # --- This returns the 3 objects as $x, $y and $z. I cannot, however, access these objects individually by typing for example: test$x Generally one assigns the resutlts of a function to an object name: ll - test(1,2) Warning message: In return(x, y, z) : multi-argument returns are deprecated ll $x [1] 1 2 $y [1] 2 $z [1] 0.5 str(ll) List of 3 $ x: int [1:2] 1 2 $ y: num 2 $ z: num 0.5 So you would not get the warnings if you instead ended the function with: return(list(x,y,z) ) I know i can do this by adding an extra arrow head to the assignment arrow (-), but I am sure that is not how it is done in some of the established R functions (like when calling lm$coef out of the lm function). Is there a simple command i've omitted from the function that allows access to objects inside it? Thanks in advance, Steve -- -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.