Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Simple solutions for the error in colmeans(x, na.rm = true) : 'x' must be numeric error
Next
Answered
Jules Forestier
  • 19
Jules Forestier
Asked: May 12, 20222022-05-12T11:33:49+00:00 2022-05-12T11:33:49+00:00In: r

Simple solutions for the error in colmeans(x, na.rm = true) : ‘x’ must be numeric error

  • 19

. Advertisement .

..3..

. Advertisement .

..4..

I am new to r and searching the “error in colmeans(x, na.rm = true) : ‘x’ must be numeric” to understand it better. It seems it doesn’t work as expected when I used some suggestions before. This is the command line I use:

data(birth.death.rates.1966)
 data2 <- birth.death.rates.1966
 princ <- prcomp(data2)

The error I’m getting is below:

Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

Please give me the solution for this issue.

error in colmeans
  • 2 2 Answers
  • 530 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-05-31T03:37:07+00:00Added an answer on May 31, 2022 at 3:37 am

    factor allows you to convert a vector of characters into numeric values. Each unique value then gets an unique integer code. This example shows four values. The numbers 1 through 4 are alphabetical.

    > d = data.frame(country=c("foo","bar","baz","qux"),x=runif(4),y=runif(4))
    > d
      country          x         y
    1     foo 0.84435112 0.7022875
    2     bar 0.01343424 0.5019794
    3     baz 0.09815888 0.5832612
    4     qux 0.18397525 0.8049514
    > d$country = as.numeric(as.factor(d$country))
    > d
      country          x         y
    1       3 0.84435112 0.7022875
    2       1 0.01343424 0.5019794
    3       2 0.09815888 0.5832612
    4       4 0.18397525 0.8049514

    Then, you can run prcomp

    > prcomp(d)
    Standard deviations:
    [1] 1.308665216 0.339983614 0.009141194
    
    Rotation:
                   PC1          PC2          PC3
    country -0.9858920  0.132948161 -0.101694168
    x       -0.1331795 -0.991081523 -0.004541179
    y       -0.1013910  0.009066471  0.994805345

    It is up to the application whether this makes sense. You might just want to remove prcomp(d[,-1]) from the first column and instead work with numeric data.

    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Gabriel Cohen
    2022-05-25T19:45:13+00:00Added an answer on May 25, 2022 at 7:45 pm

    The character is the first column in the data frame. You can also recode the data frame to create row names like:

    library(tidyverse)
    data2 %>% remove_rownames %>% column_to_rownames(var="country")
    princ <- prcomp(data2)

    Alternately, you can also use:

    data2 <- data2[,-1]
    rownames(data2) <- data2[,1]
    princ <- prcomp(data2)
    • 18
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.