A little R prank

[This article was first published on R – insightR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)