A little R prank

April 11, 2017

(This article was first published on R – insightR, and kindly contributed to R-bloggers)

By Gabriel Vasconcelos

R functions

The book Advanced R, by Hadley Wickham, shows a very interesting statement:

“To understand R, two slogans are Helpful:

  • Everything that exists in an object.
  • Everything that happens is a function call.”

– John Chambers

In other words, every action is caused by a function, and functions are things you can change. [, ( and + are all functions. That means you can call these actions as functions.

> a=1:10

> #Two equivalent codes
> `[`(a,4) 
[1] 4

> a[4]
[1] 4

> #Other two equivalent codes
> `+`(3,7)
[1] 10

> 3+7
[1] 10

Just a little prank

Imagine you want to do a little prank with your friend who left his R oppened in the office. You could change the + function like in the code below and see the magic happens.

> `+`=function(x,y)x-y
> 1+1
[1] 0

More information on R functions

[1] Wickham, Hadley. Advanced R. CRC Press, 2014.

To leave a comment for the author, please follow the link and comment on their blog: R – insightR.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)