rashid rashid - 24 days ago 4x
R Question

variable-assignment: Difference between "<-" and "=" in a certain post & avoiding using "return"

I have found the following solution on here about the post:
Extracting and formatting results of cor.test on multiple pairs of columns

The user eipi10 uses

insted of
to assign a value to the
function. Why does he do this?

Also, he/she creates the data.frame in the next line, but does not use a
to have that data.frame returned from the code. The function works, so I wonder why and how.

I tried to ask this in a comment, but I need 50 reputation to put comments and why I put an answer in the initial thread, it was immediately deleted. Could anybody tell me, how to ask about a solution I find in a threat when I can not comment and can not post an answer?

Thank you.


The first part of my question has been answered partly by the link but I still do not understand why the
is avoided. thanks :)


From ?return:

If the end of a function is reached without calling return, the value of the last evaluated expression is returned.

For example,

f <- function() {
   x <- 1

is equivalent to the same function with return(x) as the last statement. Perhaps surprisingly,

f <- function() {
   x <- 1

also returns the same value, but returns it invisibly. There is a minor schism (perhaps not quite as strong as the = vs. <- schism) about whether it's better practice to always use an explicit return(): I believe it is good practice (because it makes the intention of the code more explicit), but many old-school R programmers prefer the implicit return value.