R Error in rowSums & colSums – ‘x’ must be an array of at least two dimensions
In this tutorial, I’ll show how to handle the “error in rowSums & colSums – ‘x’ must be an array of at least two dimensions” in the R programming language.
The tutorial will contain these content blocks:
Let’s just jump right in…
Creation of Example Data
We’ll use the following data as a basis for this tutorial.
data <- data.frame(x1 = 1:5, # Create example data frame x2 = 5:1, x3 = 5) data # Print example data frame |
data <- data.frame(x1 = 1:5, # Create example data frame x2 = 5:1, x3 = 5) data # Print example data frame
Table 1 shows the structure of our example data – It is constituted of five rows and three variables. The variables x1 and x2 are integers and the variable x3 is numerical.
Example 1: Reproduce the Error – ‘x’ must be an array of at least two dimensions
The following R programming code demonstrates how to replicate the error message “‘x’ must be an array of at least two dimensions” when using the colSums function. Note that the same error message would be returned when using the rowSums function instead.
We could apply the colSums function without any problems to our example data as shown below:
colSums(data) # Column sums of entire data frame # x1 x2 x3 # 15 15 25 |
colSums(data) # Column sums of entire data frame # x1 x2 x3 # 15 15 25
However, sometimes we might want to apply the colSums or rowSums function to a subset of our data. Let’s assume that we want to apply this function only to the first column of our data set:
colSums(data[ , 1]) # Column sums for subset # Error in colSums(data[, 1]) : # 'x' must be an array of at least two dimensions |
colSums(data[ , 1]) # Column sums for subset # Error in colSums(data[, 1]) : # 'x' must be an array of at least two dimensions
Unfortunately, the error message “Error in colSums(data[, 1]) : ‘x’ must be an array of at least two dimensions” has been returned.
The reason for this is that our one-column data was automatically converted to a vector object.
Let’s solve this problem!
Example 2: Fix the Error – ‘x’ must be an array of at least two dimensions
If we want to create a data frame subset of only one column, we can use the drop argument to keep the data frame structure.
To this subset, we can then apply the colSums function without getting any error messages:
colSums(data[ , 1, drop = FALSE]) # Column sums for subset # x1 # 15 |
colSums(data[ , 1, drop = FALSE]) # Column sums for subset # x1 # 15
Looks good!
Video, Further Resources & Summary
Some time ago, I have released a video on my YouTube channel, which shows the R syntax of this article. You can find the video below.
The YouTube video will be added soon.
Also, you may want to have a look at some of the other tutorials on my website.
- Error in as.Date.numeric(X) : ‘origin’ must be supplied
- Error in barplot.default() : ‘height’ must be a vector or a matrix
- Error in hist.default : ‘x’ must be numeric
- Error : ‘names’ attribute must be the same length as the vector
- Error in sort.int(x, na.last, decreasing, …) : ‘x’ must be atomic
- Fixing Warnings & Errors in R (Overview)
- Introduction to R
To summarize: You have learned in this article how to replicate and fix the “error in rowSums & colSums – ‘x’ must be an array of at least two dimensions” in R programming. In case you have further questions, please let me know in the comments section.