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

 

table 1 data frame r colsums rowsums error must be array two dimensions

 

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

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

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

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.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top