R frustration of the day

Whenever you take a 1 column slice of a matrix, that gets automatically converted into a vector. But if you take a slice of several columns, it remains a matrix. The problem is you don’t always know in advance how big the slice will be, so if you do this:

You'll get an error if x is 1. This creates the worst kind of bug: an intermittent one that will hide until the right (wrong?) value of x occurs. To fix the problem you need to RE-declare the slice to be a matrix with ncol=x after you take the slice.


  1. Hi Stu,

    Thanks for the link. I didn’t know about the



    Every programming language has it’s quirks and hidden bogies, but R seems to have more than its fair share, a situation made worse by the documentation (something I’ll write more about in a future post).



Leave a comment