How to Fix in R: dim(X) must have a positive length
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
dataframe <- data.frame (score= c (91, 92, 87, 80, 79),
marks= c (97, 90, 81, 88, 89),
performance= c (80, 97, 86, 57, 88))
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
dataframe <- data.frame (score= c (91, 92, 87, 80, 79),
marks= c (97, 90, 81, 88, 89),
performance= c (80, 97, 86, 57, 88))
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
dataframe <- data.frame (score= c (91, 92, 87, 80, 79),
marks= c (97, 90, 81, 88, 89),
performance= c (80, 97, 86, 57, 88))
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
dataframe <- data.frame (score= c (91, 92, 87, 80, 79),
marks= c (97, 90, 81, 88, 89),
performance= c (80, 97, 86, 57, 88))
apply (dataframe[ c ( 'score' , 'marks' )], 2, mean)
|
Output:
Output
Example:
Suppose that we want to compute the mean of the “performance” column.
R
dataframe <- data.frame (score= c (91, 92, 87, 80, 79),
marks= c (97, 90, 81, 88, 89),
performance= c (80, 97, 86, 57, 88))
mean (dataframe$performance)
|
Output:
Output
Example 2
In this example, we will create a dataframe and find product of each column
R
print ( "GeeksforGeeks" )
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)
)
product = function (x, output){
A = x[1]
B=x[2]
C= x[3]
return (A*B*C)
}
cbind (dataframe,product = apply (dataframe$B,1,product))
|
OUTPUT
Now, let us try to fix it.
R
print ( "GeeksforGeeks" )
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)
)
product = function (x, output){
A = x[1]
B=x[2]
C= x[3]
return (A*B*C)
}
cbind (dataframe,product = apply (dataframe,1,product))
|
OUTPUT
Last Updated :
16 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...