drop=FALSE in R
January 11, 2021
I have not done any blog posts on coding tips/tricks, but after discovering the magic of drop=FALSE
I just had to post. Well, it’s not really magic, it just overcomes one of the most frustrating quirks of R I have come across so far.
Let’s take an example to demonstrate this quirk. Let’s take a 3x2 matrix, select the first two rows, and then take the sum of the rows. This is easy to do in R, and the following code runs just fine.
> example_matrix_1
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> example_matrix_1[1:2,]
[,1] [,2]
[1,] 1 4
[2,] 2 5
> rowSums(example_matrix_1[1:2,])
[1] 5 7
Now, one would think if the matrix we used in the above example was a 3x1 matrix the code would run. Unfortunately this is not the case.
> example_matrix_2
[,1]
[1,] 1
[2,] 2
[3,] 3
> example_matrix_2[1:2,]
[1] 1 2
> rowSums(example_matrix_2[1:2,])
Error in rowSums(example_matrix_2[1:2, ]) :
'x' must be an array of at least two dimensions
What happened? Well, in all of its infinite wisdom R decided that when we take the first two rows of our matrix example_matrix_2
we want a vector as an output, and not a 2x1 matrix. This makes no sense, but it is what R does.
So, how do we rectify this? This is where the handy drop=FALSE
command comes into play. Essentially when subsetting the one dimensional matrix we include drop=FALSE
to make the output a one dimensional matrix.
> example_matrix_2[1:2,,drop=FALSE]
[,1]
[1,] 1
[2,] 2
> rowSums(example_matrix_2[1:2,,drop=FALSE])
[1] 1 2
I only wish I had known this a year ago, and then would have avoided a great deal of frustration!