Fix the Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric in R (2 Examples)
In this tutorial, I’ll demonstrate how to avoid the “Error in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric” in R.
Creation of Example Data
data(iris) # Loading example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Loading example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
Example 1: Replicating the Error Message in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric
prcomp(iris) # prcomp function cannot be applied to character column # Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric |
prcomp(iris) # prcomp function cannot be applied to character column # Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
Example 2: Debugging the Error Message in colMeans(x, na.rm = TRUE) : ‘x’ must be numeric
iris_numb <- iris # Transforming categories to numbers iris_numb$Species <- as.numeric(as.factor(iris_numb$Species)) head(iris_numb) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 1 # 2 4.9 3.0 1.4 0.2 1 # 3 4.7 3.2 1.3 0.2 1 # 4 4.6 3.1 1.5 0.2 1 # 5 5.0 3.6 1.4 0.2 1 # 6 5.4 3.9 1.7 0.4 1 |
iris_numb <- iris # Transforming categories to numbers iris_numb$Species <- as.numeric(as.factor(iris_numb$Species)) head(iris_numb) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 1 # 2 4.9 3.0 1.4 0.2 1 # 3 4.7 3.2 1.3 0.2 1 # 4 4.6 3.1 1.5 0.2 1 # 5 5.0 3.6 1.4 0.2 1 # 6 5.4 3.9 1.7 0.4 1
prcomp(iris_numb) # Applying prcomp function to new data frame # Standard deviations (1, .., p=5): # [1] 2.1996441 0.5023804 0.3094851 0.1914559 0.1443656 # # Rotation (n x k) = (5 x 5): # PC1 PC2 PC3 PC4 PC5 # Sepal.Length 0.33402494 -0.68852577 0.4414776 -0.43312829 0.1784853 # Sepal.Width -0.08034626 -0.68474905 -0.6114140 0.30348725 -0.2423462 # Petal.Length 0.80059273 0.09713877 0.1466787 0.49080356 -0.2953177 # Petal.Width 0.33657862 0.06894557 -0.4202025 0.06667133 0.8372253 # Species 0.35740442 0.20703034 -0.4828930 -0.68917499 -0.3482135 |
prcomp(iris_numb) # Applying prcomp function to new data frame # Standard deviations (1, .., p=5): # [1] 2.1996441 0.5023804 0.3094851 0.1914559 0.1443656 # # Rotation (n x k) = (5 x 5): # PC1 PC2 PC3 PC4 PC5 # Sepal.Length 0.33402494 -0.68852577 0.4414776 -0.43312829 0.1784853 # Sepal.Width -0.08034626 -0.68474905 -0.6114140 0.30348725 -0.2423462 # Petal.Length 0.80059273 0.09713877 0.1466787 0.49080356 -0.2953177 # Petal.Width 0.33657862 0.06894557 -0.4202025 0.06667133 0.8372253 # Species 0.35740442 0.20703034 -0.4828930 -0.68917499 -0.3482135
Related Tutorials & Further Resources
You may find some related R programming tutorials on topics such as coding errors and ggplot2 below.