Adding missing rows

前端 未结 3 1619
旧巷少年郎
旧巷少年郎 2020-12-06 03:26

The format of my excel data file is:

 day                 value
 01-01-2000 00:00:00    4
 01-01-2000 00:01:00    3
 01-01-2000 00:02:00    1
 01-01-2000 00:         


        
相关标签:
3条回答
  • 2020-12-06 03:36

    This is now completely automated in the padr package. Takes only one line of code.

    original <- data.frame(
      day = as.POSIXct(c("01-01-2000 00:00:00",
                         "01-01-2000 00:01:00",
                         "01-01-2000 00:02:00",
                         "01-01-2000 00:04:00"), format="%m-%d-%Y %H:%M:%S"),
      value = c(4, 3, 1, 1))
    
    library(padr)
    library(dplyr) # for the pipe operator
    original %>% pad %>% fill_by_value(value)
    

    See vignette("padr") or this blog post for its working.

    0 讨论(0)
  • 2020-12-06 03:37

    Try:

    ts = read.csv(file=pathfile, header=TRUE, sep=",", stringsAsFactors=F)
    ts.tmp = rbind(ts,list("01-01-2000 00:03:00",0))
    ts.out = ts.tmp[order(ts.tmp$day),]
    

    Notice that you need to force load the strings in first column as character and not factors otherwise you will have issue with the rbind. To get the day column to be a factor after than just do:

    ts.out$day = as.factor(ts.out$day)
    
    0 讨论(0)
  • 2020-12-06 03:50

    I think this is a more general solution, which relies on creating a sequence of all timestamps, using that as the basis for a new data frame, and then filling in your original values in that df where applicable.

    # convert original `day` to POSIX
    ts$day <- as.POSIXct(ts$day, format="%m-%d-%Y %H:%M:%S", tz="GMT")
    
    # generate a sequence of all minutes in a day
    minAsNumeric <- 946684860 + seq(0,60*60*24,by=60) # all minutes of your first day
    minAsPOSIX <- as.POSIXct(minAsNumeric, origin="1970-01-01", tz="GMT") # convert those minutes to POSIX
    
    # build complete dataframe
    newdata <- as.data.frame(minAsPOSIX)
    newdata$value <- ts$value[pmatch(newdata$minAsPOSIX, ts$day)] # fill in original `value`s where present
    newdata$value[is.na(newdata$value)] <- 0 # replace NAs with 0
    
    0 讨论(0)
提交回复
热议问题