John
John

Reputation: 11

Creating box plot on exercise

I am trying to create a box plot on exercise:

boxplot(exercise, data=exercise, main="Outlier Analysis Exercise")

Error in oldClass(stats) <- cl : 
  adding class "factor" to an invalid object

I get those error message. It is in survey_fixed file and I already attached it. exercise column has the value among these three: none, some or freq.

DATA SAMPLE:

exercise

some none none none some freq freq freq

I wonder what I did wrong? and how to fix it?

Upvotes: 0

Views: 5045

Answers (1)

Peter_Evan
Peter_Evan

Reputation: 947

The error message is telling you the issue: you can't use only factors to create a box plot. boxplot is looking for a numeric vector. Run code below as an example:

df <- data.frame(
"age" = c(77,74,55,62,60,59,32,91,75,73,43,67,58,18,57),
"party" = c("Independent", "Independent", "Independent", "Democrat", 
          "Independent", "Republican", "Independent", 
          "Independent", "Democrat", "Republican", "Republican", 
          "Democrat", "Democrat", "Independent", "Independent"),
)

df$party <- as.factor(df$party)
df$age <- as.numeric(df$age)

boxplot(df$party) # gives same error
boxplot(df$age) #runs

see ?boxplot for examples on using formulas in the boxplot function, as that may be what you are looking for? For example:

 boxplot(df$age~df$party)

Upvotes: 2

Related Questions