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 <[email protected]>
Sent: Friday, July 28, 2017 12:25:54 PM
To: Vijaya Kumar Regati
Cc: [email protected]; [email protected]
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"
<[email protected]<mailto:[email protected]>> 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]]
______________________________________________
[email protected]<mailto:[email protected]> 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]]
______________________________________________
[email protected] 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.