简体   繁体   中英

How can I fill missing data points in R for a given dataframe

I have a dataframe which contains dates, products and amounts. However product b is not on every date, I would like it to be with an NA or 0 balance. Is this possible?

Summary_Date <- 
  as.Date(c("2017-01-31",
        "2017-02-28",
        "2017-03-31",
        "2017-03-31",
        "2017-04-30",
        "2017-05-31",
        "2017-05-31",
        "2017-06-30"))
Product <-
  as.character(c("a","a","a","b","a","a","b","a"))
Amounts <-
  as.numeric(c(10,10,10,20,10,10,20,10))

df <- data.frame(Summary_Date,Product,Amounts)

Regards, Aksel

You can use tidyr :

> library(tidyr)
> complete(data = df,Summary_Date,Product)
# A tibble: 12 x 3
   Summary_Date Product Amounts
         <date>  <fctr>   <dbl>
 1   2017-01-31       a      10
 2   2017-01-31       b      NA
 3   2017-02-28       a      10
 4   2017-02-28       b      NA
 5   2017-03-31       a      10
 6   2017-03-31       b      20
 7   2017-04-30       a      10
 8   2017-04-30       b      NA
 9   2017-05-31       a      10
10   2017-05-31       b      20
11   2017-06-30       a      10
12   2017-06-30       b      NA

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM