Good afternoon,
I'm analyzing the distribution of observations in a given month, for example:
Date Observations
2010-01 10
2010-03 15
2010-05 16
Question: How do I insert the missing dates (2010-02 and 2010-05) in the table (using other table with all the monthly dates) and attribute a 0 as observations.
Thanks in advance.
We convert the 'Date' to Date
class, then use complete
expand the dataset by getting the sequence of min/max
or first
, last
'Date' by
'1 month' while fill
ing the 'Observations' with 0
library(tidyr)
library(dplyr)
df1 %>%
mutate(Date = as.Date(Date)) %>%
complete(Date = seq(first(Date), last(Date), by = '1 month'),
fill = list(Observations = 0))
If there is another dataset with complete 'Date', then the obvious option is a left_join
and then replace the NA
elements in 'Observations' with 0 because by default if we don't have a match, it will be filled with NA
left_join(df2, df1, by = 'Date') %>%
mutate(Observations = replace_na(Observations, 0))
NOTE: df2
is the dataset with complete 'Date'
In case, if the 'df2' have other columns as well, we don't need to select
those columns
left_join(df2 %>%
select(Date), df1) %>%
mutate(Observations = replace_na(Observations, 0))
In base R
, we can use merge
transform(merge(df2, df1, by = 'Date', all.x = TRUE),
Observations = replace(Observations, is.na(Observations), 0))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.