ima
ima

Reputation: 155

R: Weekday recognized as character : how to convert it to a numerical value

I'm new to R and I'm trying to build a histogram that shows the frequency of datapoints for each day of the week (so frequency of data on monday, tuesday ect). Since the raw data set contains information on the date and time in the form of YYYY-MM-DD HH:MM:SS I initially transformed the date to a numerical value of the day of the week with the script:

Weekday <- format(as.POSIXct(strptime(df$DateAndTime, "%Y-%m-%d %H:%M:%S", 
tz="")),format = "%w"))
Data$Weekday <- Weekday

I'm receiving the following message when trying to create a histogram:

hist(Data$Weekday)
Error in hist.default(Data$Weekday) : 'x' must be numeric

In the data file the data for Weekday is in number (0-6, where 0 is Sunday), from the summary the class and mode are both characters. How do I change a numerical data point (representing a weekday) that is recognized by R as a character into a numerical number?

Thank you!

Upvotes: 0

Views: 1484

Answers (1)

Julia Silge
Julia Silge

Reputation: 11613

I really like using the lubridate package when dealing with weekdays. You can go straight from the YYYY-MM-DD HH:MM:SS format you say you have to weekdays with the wday() function, and it plays very nicely with ggplot2.

library(readr)
library(lubridate)
library(ggplot2)

example_data <- read_csv("datetime\n
                         2016-12-03 00:01:00\n
                         2016-12-03 00:02:00\n
                         2016-12-04 00:03:00\n
                         2016-12-04 00:04:00\n
                         2016-12-05 00:05:00\n
                         2016-12-06 00:04:00\n
                         2016-12-07 00:05:00\n
                         2016-12-08 00:04:00\n
                         2016-12-09 00:04:00\n
                         2016-12-11 00:05:00\n
                         2016-12-12 00:06:00\n
                         2016-12-13 00:07:00\n
                         2016-12-13 00:08:00\n
                         2016-12-14 00:09:00\n
                         2016-12-14 00:10:00")

ggplot(example_data, aes(wday(datetime, label = TRUE))) +
    geom_histogram(stat = "count") +
    labs(x = NULL)

Upvotes: 2

Related Questions