How to Solve R Error in colSums – ‘x’ must be an array of at least two dimensions

by | Programming, R, Tips

This error occurs when you try to pass a 1-dimensional vector to the colSums function, which expects a 2-dimensional input. If we want to subset a data frame column, we can use the drop argument to preserve the data frame object. For example,

df <- data.frame(x1 = rnorm(10),         
                   x2 = rnorm(10),
                   x3 = rnorm(10))

colSums(df[,1, drop=FALSE])

This tutorial will go through the error and how to solve it with code examples.


Example

Let’s look at an example to reproduce the error:

# Create a data frame

df <- data.frame(x = sample.int(100, 10),
                 y = sample.int(50, 10),
                 z = sample.int(200,10))

df
    x  y   z
1  56 39  24
2  17 32 166
3  26 15  87
4  12 35 189
5  23  3 156
6  59 33 144
7  33 17 170
8  44 47 157
9  67  6 126
10 64 49  75

Next, we will attempt to sum up the values in the first column using the colSums function:

first_col_sum <- colSums(df[, 1])

Let’s run the code to see what happens:

Error in colSums(df[, 1]) : 
  'x' must be an array of at least two dimensions

The error occurs because df[, 1] is a 1-dimensional vector:

df[,1]
 [1] 56 17 26 12 23 59 33 44 67 64

However, the colSums function expects an array of two or more dimensions.

Solution

We can solve the error by subsetting the column with the drop argument set to FALSE. By specifying drop as FALSE we ensure that R does not convert the column to a vector object.

Let’s look at the revised code:

# Extract column from data frame

col <- df[, 1, drop = FALSE]

# Check object is 2-dimensional

dim(col)

# Check object is a data frame

is.data.frame(col)
[1] 10  1
[1] TRUE

Let’s use the colSums function to get the sum of the column values:

# Attempt to get the sum of the column

first_col_sum <- colSums(col)

first_col_sum
401

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee