Hacker News new | ask | show | jobs
by numlocked 3778 days ago
At my previous job we used to play "Guess what R does" over lunch. Someone would write a few R statements and we'd have to guess the output. Extremely difficult!

    >> a=c(1,2,3,4)
    >> b=c(1,2)
    >> a+b
Any guesses?
2 comments

+ is a vector operation, and does it elementwise. R recycles vectors. You get warnings if there are elements left over. This is something you should know within the first 5 minutes of learning the language, hopefully?
2, 4, 4, 6. The second vector is recycled along the first one:

1 + 1, 2 + 2, 3 + 1, 4 + 2.