Converting 20110101 to 1

It's my first time using R so my question might be quite silly.

I have this data starting from 20110101 and ends at 20111231
It's integer not date, and I want to change it to numerics starting from 1 to 365.

The reason why is that when I use barplot or ggplot2, there is a gap such as 20110190 or so.

The data has 4 columns and 200,000 lines.
My final goal is to get the mean and sd of each month and week.

Please help. Thank you

For all the things you want to do, it would be easier if you work with dates instead of integers, try to do something like this.

library(dplyr)
library(lubridate)
library(ggplot2)

sample_data <- data.frame(date = c(20110101L, 20111231L, 20120105L),
                          value = runif(3, 0, 20))
# Converting integers to dates
sample_data <- sample_data %>%
    mutate(date = ymd(date))
# Using dates with ggplot
sample_data %>% 
    ggplot(aes(x = date, y = value)) +
    geom_col() +
    scale_x_date(date_breaks = '1 month', date_labels = '%Y-%m') +
    theme_bw() +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))

Created on 2019-02-08 by the reprex package (v0.2.1)

We could give you better help if you provide a REPRoducible EXample (reprex) If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

This topic was automatically closed 21 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.