[英]Padding around dates in R to add missing/blank months?
The padr R pacakge vignette describes different package functions to pad dates and times around said dates and times. padr R 包装小插图描述了不同的 package 函数以在所述日期和时间周围填充日期和时间。
I am in situations where I'll be tallying events in data frames (ie dplyr::count()
) and will need to plot occurrences, over a period of say... 1 year.我的情况是,我将在数据帧中记录事件(即dplyr::count()
)并且需要 plot 事件,在一段时间内... 1 年。 When I count the events in a low volume data frame I'll often get single line item results, like this:当我在低容量数据框中计算事件时,我经常会得到单行项目结果,如下所示:
library(tidyverse)
library(lubridate)
library(padr)
df <- tibble(col1 = as.Date("2018-10-01"), col2 = "g", col3 = 5)
#> # A tibble: 1 x 3
#> col1 col2 col3
#> <date> <chr> <dbl>
#> 1 2018-10-01 g 5
To plot this with ggplot, over a period of a year, on a monthly basis, requires a data frame of 12 rows.对于 plot 这与 ggplot,在一年的时间里,每月需要一个 12 行的数据框。 It basically needs to look like this:它基本上需要看起来像这样:
#> # A tibble: 12 x 3
#> col1 col2 col3
#> <date> <chr> <dbl>
#> 1 2018-01-01 NA 0
#> 2 2018-02-01 NA 0
#> 3 2018-03-01 NA 0
#> 4 2018-04-01 NA 0
#> 5 2018-05-01 NA 0
#> 6 2018-06-01 NA 0
#> 7 2018-07-01 NA 0
#> 8 2018-08-01 NA 0
#> 9 2018-09-01 NA 0
#> 10 2018-10-01 g 5
#> 11 2018-11-01 NA 0
#> 12 2018-12-01 NA 0
Perhaps padr()
can do this with some combination of the thicken()
and pad()
functions.或许padr()
可以通过一些thicken()
和pad()
函数的组合来做到这一点。 My attempts are shown below, neither line 3 nor line 4 construct the data frame shown directly above.我的尝试如下所示,第 3 行和第 4 行都没有构建上面直接显示的数据框。
How do I construct that data frame direclty above, utilizing padr()
, lubridate()
, tidyverse()
, data.table()
, base R
, or any way you please?如何使用padr()
、 lubridate()
、 tidyverse()
、 data.table()
、 base R
或任何你喜欢的方式直接构建上面的数据框? Manual entry of each month shall not be considered either, if that needs to be said.如果需要说明,也不应考虑手动输入每个月。 Thank you.谢谢你。
df %>%
thicken("year") %>%
# pad(by = "col1") %>% # line 3
# pad(by = "col1_year") %>% # line 4
print()
library(lubridate)
library(tidyverse)
df <- tibble(col1 = as.Date("2018-10-01"), col2 = "g", col3 = 5)
my_year <- year(df$col1[1])
df2 <- tibble(col1 = seq(ymd(paste0(my_year,'-01-01')),ymd(paste0(my_year,'-12-01')), by = '1 month'))
df3 <- merge(df,df2, by ="col1",all.y=TRUE) %>% mutate(col3 = replace_na(col3,0))
df3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.