简体   繁体   中英

R - insert rows for missing days in data frame

I have a data frame as follows:

> head(train)
      S    D       Date
1     1    1 2010-02-05
2     1    1 2010-02-12
3     1    1 2010-02-19

The Date column has only one date per week, and for each present date I would like to insert 6 rows for all missing days after the mentioned date. So the result would look like:

> head(train)
      S    D       Date
1     1    1 2010-02-05
1     1    1 2010-02-06 <- inserted
1     1    1 2010-02-07 <- inserted
1     1    1 2010-02-08 <- inserted
1     1    1 2010-02-09 <- inserted
1     1    1 2010-02-10 <- inserted
1     1    1 2010-02-11 <- inserted
2     1    1 2010-02-12
etc

Probably overkill, but the point is to do a join on the dates with the "correct" dates then fill in:

library(dplyr)
library(zoo)

train <- data.frame(D = 1:3, S = 4:6, Date = as.Date("2010-02-05") + 7*(1:3))
full.dates <- as.Date(min(train$Date):max(train$Date), origin = "1970-01-01")
db <- data.frame(Date = full.dates)
fixed <- left_join(db, train)

# Fill from top using zoo::na.locf
fixed[ ,c("D", "S")] <- na.locf(fixed[ ,c("D", "S")])

Another way of using na.locf in package zoo , where you create a zoo time series, and use the xout argument in na.locf . xout specifies which date range to use for extra-/interpolation.

library(zoo)

# either convert raw data to zoo object
z <- read.zoo(text = "S    D       Date
1     1    1 2010-02-05
2     1    1 2010-02-12
3     1    1 2010-02-19", index.column = "Date")

# ...or convert your data frame to zoo
z <- zoo(x = df[ , c("S", "D")], order.by = df$Date)

# create a sequence of dates, from first to last date in original data
tt <- seq(from = min(index(z)), to = max(index(z)), by = "day")

# expand time series to 'tt', and replace each NA with the most recent non-NA prior to it
na.locf(z, xout = tt)

#            S D
# 2010-02-05 1 1
# 2010-02-06 1 1
# 2010-02-07 1 1
# 2010-02-08 1 1
# 2010-02-09 1 1
# 2010-02-10 1 1
# 2010-02-11 1 1
# 2010-02-12 1 1
# 2010-02-13 1 1
# 2010-02-14 1 1
# 2010-02-15 1 1
# 2010-02-16 1 1
# 2010-02-17 1 1
# 2010-02-18 1 1
# 2010-02-19 1 1

Being a simpleton:-),

library(lubridate)
train
#  D S       date
# 1 1 2 2010-02-05
# 2 1 3 2010-02-12
 ttmp<-train[1,]
 for(j in 1:6) ttmp<-rbind(ttmp,train[1,])
 for(j in 2:7) ttmp[j,3]<-ttmp[j-1,3]+ddays(1)
 ttmp
#  D S       date
# 1 1 2 2010-02-05
# 2 1 2 2010-02-06
# 3 1 2 2010-02-07
# 4 1 2 2010-02-08
# 5 1 2 2010-02-09
# 6 1 2 2010-02-10
# 7 1 2 2010-02-11

newtrain<-rbind(train[1,],ttmp)

Then loop over all your initial lines and rbind it all together.

You can get the number of missing rows by:

nMiss <- diff(as.Date(train$Date))

You can then repeat each row of the data.frame the relevant number of times:

longTrain <- train[rep(1:nrow(train), times=c(nMiss, 1)),]

You can generate a date offset along the lines of:

off <- unlist(lapply(c(nMiss,1)-1, seq, from=0)
longTrain$Date <- as.Date(longTrain$Date)+off

If you want to add extra rows at the end of the data frame, you can change the constant 1 in c(nMiss, 1) to the relevant number.

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