tdsmith / aRrgh

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

discuss dimension dropping #13

Open kevinushey opened 10 years ago

kevinushey commented 10 years ago
m <- matrix(1:9, nrow=3)

class( m[1:2, ] ) ## matrix!
class( m[1, ] ) ## integer!

Useful when you're expecting it, the cause of difficult to trace bugs when you're not. The terribly wonderful syntax

m[1, , drop=FALSE]

allows you to avoid dimension dropping explicitly.

kevinushey commented 10 years ago

Similarily, why apply is evil (always drops dimensions if the function applied returns a scalar*)

*vector of length one