tdsmith / aRrgh

A newcomer's (angry) guide to data types in R
Other
306 stars 14 forks source link

indexing gotcha #19

Open davharris opened 9 years ago

davharris commented 9 years ago

This burned me a few months back.

f = function(x){
x
}

f() # Throws error: x is missing

g = function(x){
letters[x]
}

g(1) # Returns the first element of letters
g() # Doesn't throw error, returns ALL elements of letters 

Hadley Wickham pointed out that g(x) is doing helpfully letters[], which is different from letters[NULL].

ಠ_ಠ