R. Biggest surprise.





y <- 0
x <- function() {
  y <- 5
}

x()
print(y)

In the above code, Every object programmer or C programmer would expect, value of y to be printed as 5 after this code block gets executed. But it doesnt work this way in R.

If you want to update global variable in R inside a function u have to use <<- instead of <- as assignment inside the function.

so,

x <- function() {
  y <<- 5
}

Would update the global variable y correctly :-). Was a big surprise for me.

Comments

Popular posts from this blog