A little R prank

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.

Advertisements
This entry was posted in R and tagged , , , . Bookmark the permalink.

One Response to A little R prank

  1. see the magic happens? hahahaha awesome!

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s