r/R_Programming • • Sep 30 '17

R recognizing numbers as a categorical variable instead of numeric-how to fix?

Hi guys. I have a homework assignment for a stats class that has us using the the 'Swiss' data set on R. I need to show the distribution for education and describe it with appropriate statistics, but it seems as if its not an numeric variable in R.

Here is the code I used to try to create a histogram:

hist(swiss$education, xlab= "Portion of Population Educated (Percent)", ylab="Frequency", main="Distribution of Population Educated in Switzerland 1888", right=F, col="blue")

Error in hist.default(swiss$education, xlab = "Portion of Population Educated (Percent)", : 'x' must be numeric

I also tried to take the mean and standard deviation:

mean(swiss$education) sd(swiss$education)

and got >>NA for both.

Is there any way for me to convert this to a numeric variable? To my understanding, these are both percent values showing the percent of males that were in agriculture or educated.

Thanks!

2 Upvotes

7 comments sorted by

View all comments

2

u/engineeret Sep 30 '17 edited Sep 30 '17

Did you try to see what kind of dataset you have. For example try str(swiss$education) and see if it is numeric.

Edit: I just loaded the dataset and seems to be working. I was able to make a histogram, find the mean and standard deviation. Maybe try to close the program and load the data again.

1

u/SwamiJesus Sep 30 '17

I just didn't capitalize the "e" in "Education." I'm normally pretty good about that. Thanks for the response though!

1

u/engineeret Sep 30 '17

Try using R Studio. It would have popped up options for you that are available and you can pick instead of typing in the word, “Education”.