Type of vector - str and class do not match

Hi, I tried to create a histogram but when running the code I received the message:

hist("Year")
Error in hist.default("Year") : 'x' must be numeric

When I checked the structure of my data frame, "Year" is defined as num , but when I check with class("Year") is said it is a character vector (code copied below). Can anyone explain why this happens and how can I change that? Thanks

str(Social_Enterprises_Trial)
tibble [287 × 6] (S3: tbl_df/tbl/data.frame)
Name : chr [1:287] ... County : num [1:287] 22 6 7 16 20 16 7 7 5 5 ...
Location : num [1:287] 1 1 1 2 1 2 1 1 1 2 ... Year : num [1:287] 1990 2015 2022 2007 1987 ...

class("Year")
[1] "character"

"Year" (among quotes) is not referring to a vector stored in memory, it is literally a character string. I think you actually want to do this instead

hist(Social_Enterprises_Trial$Year)

Have in mind that Year is a column in the Social_Enterprises_Trial data frame, it doesn't exist as an independent object on its own.

Great, thanks for your help!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.