Home > Back-end >  Transform interval dates to one column including NA
Transform interval dates to one column including NA

Time:01-27

I have the following dataframe df (dput below):

> df
  group      date1      date2 value
1     A 2022-01-01 2022-01-03     3
2     A 2022-01-03 2022-01-06    NA
3     A 2022-01-06 2022-01-07     2
4     A 2022-01-07 2022-01-10     2
5     B 2022-01-01 2022-01-02    NA
6     B 2022-01-02 2022-01-04     1
7     B 2022-01-04 2022-01-06    NA
8     B 2022-01-06 2022-01-09     4

I would like to transform the interval date1 and date2 to one column such that it is possible to visualize. For example in row 1 we have value with date1 and date2, so the next row should have the date of date2 to date1 with the value. Th next row is NA which means nothing should happen in between. Here is the desired output:

   group      date1      date2 value
1      A 2022-01-01 2022-01-03     3
2      A 2022-01-03 2022-01-03     3
3      A 2022-01-03 2022-01-06    NA
4      A 2022-01-06 2022-01-07     2
5      A 2022-01-07 2022-01-10     2
6      A 2022-01-10 2022-01-10     2
7      B 2022-01-01 2022-01-02    NA
8      B 2022-01-02 2022-01-04     1
9      B 2022-01-04 2022-01-04     1
10     B 2022-01-04 2022-01-06    NA
11     B 2022-01-06 2022-01-09     4
12     B 2022-01-09 2022-01-09     4

As you can see it is transformed that it is possible to visualize like this:

library(ggplot2)
ggplot(df, aes(x = date1, y = value, color = group, group = group))  
  geom_step()

Created on 2023-01-26 with enter image description here

CodePudding user response:

As a suggestion, here's another way of showing the data. Adding points to show data that is not connected with anything, maybe a cleaner way to represent the original data. In total there are 3 data points for group A and 2 for group B.

ggplot(df %>% 
    mutate(Date = paste(date1, date2)), 
    aes(x = Date, y = value, color = group, group = group))   
  geom_step()   
  geom_point(aes(Date, value, color = group))   
  theme(axis.text.x = element_text(angle = 65, vjust = 1, hjust=1))
Warning messages:
1: Removed 1 row containing missing values (`geom_step()`). 
2: Removed 3 rows containing missing values (`geom_point()`).

step plot and points plot

  • Related