[R] R Programming help needed - Returning dataframes + 2 Variables dynamically

Bert Gunter bgunter.4567 at gmail.com
Fri Jul 28 16:48:11 CEST 2017


You really really need to stop posting and spend some time with a good
R tutorial or two (there are many good ones on the web). While this
list can certainly provide you some help, it cannot and is not meant
to substitute for you doing such basic homework on your own.

At least IMHO.

Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jul 28, 2017 at 1:13 AM, Vijaya Kumar Regati
<VijayaKumar.Regati at m3bi.com> wrote:
> Hi,
>
>
> That was very useful information. Thanks.
>
> But still I am not able to get the desired output with updated code.
>
> Kindly help if you have any further thoughts ...
>
> Updated code :
> x <- 0
> y <- 0
>
> Logic_fn <- function(x,y){
>
> print("Passed Values")
> print(x)
> print(y)
> x <- x + 1
> y <- y + 1
>
> print("After addition :")
> print(x)
> print(y)
>
> test_data <- rbind(x,y)
> test_data <- data.frame(test_data)
> return(list(x,y,test_data))
> }
>
> for ( i in 1:1 ) {
> test_data <- Logic_fn(x,y)
> print("Returned Values :")
> print(x)
> print(y)
> }
>
>
> Wrong output :
> [1] "Passed Values"
> [1] 0
> [1] 0
> [1] "After addition :"
> [1] 1
> [1] 1
> [1] "Returned Values :"
> [1] 0
> [1] 0
>
>
> With Regards,
> Vijaya Kumar Regati
> Technical Lead, M3bi India Private Ltd
> Work: 040-67064732
>
> ________________________________
> From: Koustav Pal <koustavpal.devel at gmail.com>
> Sent: Friday, July 28, 2017 12:25:54 PM
> To: Vijaya Kumar Regati
> Cc: vijaykr.sas at gmail.com; r-help at R-project.org
> Subject: Re: [R] R Programming help needed - Returning dataframes + 2 Variables dynamically
>
> c() is used for constructing vectors. Or in other words using the method c(x,y) provides a vector of length 2 (hopefully). Therefore, you are pushing a single vector to your function and not two arguments as you want.
>
> It should be Logic_fn(x,y) not Logic_fn(c(x,y)).
>
> Furthermore, i would recommend reading up on how function constructs work. Two return statements within a function are redundant because the function will exit after the first return statement, and anything else just creates more ambiguity. If you want to return the x y variables as well, you should use a list like so, list(test_data, x, y) and then retrieve information from the list like this Boo[[1]].....
>
> Finally i would also like to point out that you should note R's behaviour unless explicitly stated while calling the function is to assign the first argument to the first declared variable and the second to the second. So, if you called Logic_fn(y,x), then y will be assigned to x and x to y. So to avoid such a scenario you can mention it explicitly as Logic_fn(y=y,x=x).
>
>
> On Jul 28, 2017 8:39 AM, "Vijaya Kumar Regati" <VijayaKumar.Regati at m3bi.com<mailto:VijayaKumar.Regati at m3bi.com>> wrote:
> Hi,
>
>
> Can someone please help me on below issue I am facing :
>
>
> I am trying to play with returning a dataframe+2 variables using a fn.
> But facing an issue :
>
> Error in Logic_fn(c(x, y)) : argument "y" is missing, with no default
>
> This is the code I am using :
>
>
> x <- 0
> y <- 0
>
> Logic_fn <- function(x,y){
> x <- x + 1
> y < y + 1
> test_data <- rbind(x,y)
> test_data <- data.frame(test_data)
> return(test_data)
> return(c(x,y))
> }
>
> for ( i in 1:1) {
>   test_data <- Logic_fn(c(x,y))
> test_data[1]
> test_data[2]
> test_data
>
> }
>
> With Regards,
> Vijaya Kumar Regati
>
>
> Disclaimer: IMPORTANT NOTICE: This e-mail (including any attachments) are confidential, may contain proprietary or privileged information and is intended for the named recipient(s) only. If you are not the intended recipient, any disclosures, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this email in error, please notify the sender by return e-mail or telephone immediately and permanently delete the message and any attachments.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org<mailto: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.
>
> Disclaimer: IMPORTANT NOTICE: This e-mail (including any attachments) are confidential, may contain proprietary or privileged information and is intended for the named recipient(s) only. If you are not the intended recipient, any disclosures, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this email in error, please notify the sender by return e-mail or telephone immediately and permanently delete the message and any attachments.
>
>         [[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.



More information about the R-help mailing list