No Pointers in R
R does not have variables corresponding to pointers or references like those of, say, the C language. This can make programming more difficult in some cases. (As of this writing, the current version of R has an experimental feature called reference classes, which may reduce the difficulty.)
For example, you cannot write a function that directly changes its arguments. In Python, for instance, you can do this:
>>> x = [13,5,12] >>> x.sort() >>> x [5, 12, 13]
Here, the value of x
, the argument to sort()
, changed. By contrast, here’s how it works in R:
> x <- c(13,5,12) > sort(x) [1] 5 12 13 > x [1] 13 5 12
The argument to sort()
does not change. If we do want x
to change in this R code, the solution is to reassign the arguments:
> x ...
Get The Art of R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.