Open In App

How to Fix in R: dim(X) must have a positive length

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we focus on how we can fix the “dim(X) must have a positive length” error in the R programming language. 

dim(X) must have a positive length:

This is a kind of error thrown by an R compiler. The R compiler produces the error in the form:

Error in apply(dataframe$column_header1, numeric_value, mean) : 

dim(X) must have a positive length 

R compiler produces such an error when we use the apply() function to compute some value for a column of a data frame but instead of the data frame, a vector is passed as an argument. 

When this error might occur:

Let’s firstly create a data frame having three columns in it:

Example:

R




# Create a data frame
dataframe <- data.frame(score=c(91, 92, 87, 80, 79),
                 marks=c(97, 90, 81, 88, 89),
                 performance=c(80, 97, 86, 57, 88))
 
# Print data frame
dataframe


Output:

Now consider that we want to use apply() function to compute the mean value of the “marks” column:

Example:

With this example, the R compiler produces this error because the apply() function can be applied on a data frame or matrix only but here we are using it on a particular column.

R




# Create a data frame
dataframe <- data.frame(score=c(91, 92, 87, 80, 79),
                 marks=c(97, 90, 81, 88, 89),
                 performance=c(80, 97, 86, 57, 88))
 
 
# Try to calculate mean of 'points' column
apply(dataframe$marks, 2, mean)


Output:

How to fix the error:

We can fix this error by simply passing the name of the data frame to the apply() function instead of passing a particular column.

Example:

This example compiles successfully. The output represents the mean value of every column. To compute the mean values of selected columns we can explicitly specify the column name in the apply() function.

R




# Create a data frame
dataframe <- data.frame(score=c(91, 92, 87, 80, 79),
                 marks=c(97, 90, 81, 88, 89),
                 performance=c(80, 97, 86, 57, 88))
 
# Try to calculate mean of 'points' column
apply(dataframe, 2, mean)


Output:

Output

Example:

Under this example, if we want to determine the mean of a single column then we can use the mean() function instead of apply() function in R.

R




# Create a data frame
dataframe <- data.frame(score=c(91, 92, 87, 80, 79),
                 marks=c(97, 90, 81, 88, 89),
                 performance=c(80, 97, 86, 57, 88))
 
# Compute the mean of 'score' and 'marks'
# columns of the data frame
apply(dataframe[c('score', 'marks')], 2, mean)


Output:

Output

Example:

Suppose that we want to compute the mean of the “performance” column.

R




# Create a data frame
dataframe <- data.frame(score=c(91, 92, 87, 80, 79),
                 marks=c(97, 90, 81, 88, 89),
                 performance=c(80, 97, 86, 57, 88))
 
# Compute the mean of 'performance' column
mean(dataframe$performance)


Output:

Output

Example 2

In this example, we will create a dataframe and find product of each column

R




print("GeeksforGeeks")
# Create a data frame
dataframe <- data.frame(A=c(5, 6, 7, 5, 6, 9),B= c(6, 4, 3, 4, 2, 6),C= c(1, 2, 3, 7, 8, 9)
)
 
#creating a product function
product = function(x, output){
 
# accessing elements from first column
A = x[1]
 
# accessing elements from second column
B=x[2]
 
# accessing elements from third column
C= x[3]
 
# return product
return(A*B*C)
}
 
cbind(dataframe,product = apply(dataframe$B,1,product))


OUTPUT

 

Now, let us try to fix it.

R




print("GeeksforGeeks")
# Create a data frame
dataframe <- data.frame(A=c(5, 6, 7, 5, 6, 9),B= c(6, 4, 3, 4, 2, 6),C= c(1, 2, 3, 7, 8, 9)
)
 
#creating a product function
product = function(x, output){
 
# accessing elements from first column
A = x[1]
 
# accessing elements from second column
B=x[2]
 
# accessing elements from third column
C= x[3]
 
# return product
return(A*B*C)
}
 
cbind(dataframe,product = apply(dataframe,1,product))


OUTPUT

 



Last Updated : 16 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads