延长R中的data.frames日期?

问题描述

我想将LRESULT CALLBACK FloatTextBoxWindowProc(HWND windowHandle,UINT msg,WParaM wparam,LParaM lparam,UINT_PTR subclassId,DWORD_PTR refData) { if (msg == WM_CHAR) { // If the character isn't a digit or a dot,rejecting it. if (!( (wparam >= '0' && wparam <= '9') || wparam == '.' || wparam == VK_RETURN || wparam == VK_DELETE || wparam == VK_BACK || wparam == 0x03 || // CTRL-C wparam == 0x16 || // CTRL-V wparam == 0x18) // CTRL-X ) { return 0; } if (wparam == '.') // If the digit is a dot,we want to check if there already is one. { TCHAR buffer[16]; SendMessage(windowHandle,WM_GETTEXT,16,(LParaM)buffer); // _tcschr finds the first occurence of a character and returns NULL if it wasn't found. Rejecting this input if it found a dot. if (_tcschr(buffer,TEXT('.')) != NULL) { return 0; } } } return DefSubclassproc(windowHandle,msg,wparam,lparam); } 的{​​{1}}到user.name的{​​{1}}到变量extend的最后一个值重复到{{1} }。任何建议将不胜感激。

Datetime

解决方法

样本数据,仅需几行即可证明:

set.seed(123)
DF1 <- data.frame(
  Datetime = seq(as.POSIXct("2011-01-01 00:00:00"),to = as.POSIXct("2011-01-01 03:00:00"),by = "hour"),X = runif(4,5,10)
)
End_date <- as.POSIXct("2011-01-01 07:00:00")
DF1
#              Datetime        X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087

附加框架只是使用data.frame中的最后一个值对seq / DF1的另一个调用:

DF1aug <- data.frame(
  Datetime = seq(from = DF1$Datetime[nrow(DF1)],to = End_date,by="hour")[-1],X = DF1$X[nrow(DF1)]
)
DF1aug
#              Datetime        X
# 1 2011-01-01 04:00:00 9.415087
# 2 2011-01-01 05:00:00 9.415087
# 3 2011-01-01 06:00:00 9.415087
# 4 2011-01-01 07:00:00 9.415087

使用rbind可以很容易地将其扩展到第一个:

rbind(DF1,DF1aug) 
#              Datetime        X
# 1 2011-01-01 00:00:00 6.437888
# 2 2011-01-01 01:00:00 8.941526
# 3 2011-01-01 02:00:00 7.044885
# 4 2011-01-01 03:00:00 9.415087
# 5 2011-01-01 04:00:00 9.415087
# 6 2011-01-01 05:00:00 9.415087
# 7 2011-01-01 06:00:00 9.415087
# 8 2011-01-01 07:00:00 9.415087

如果您使用的是tidyverse,那么所有这些都可以与:

library(dplyr)
DF1 %>%
  slice(n()) %>%
  do(data.frame(
    Datetime = seq(from = .$Datetime,X = .$X
  )) %>%
  bind_rows(DF1,.)
,

您可以将completefill结合使用:

library(tidyr)
DF1 %>%
   complete(Datetime = seq(min(Datetime),as.POSIXct(End_date),'hour')) %>%
   fill(X)

或者仅使用complete,我们可以在DF1$X中传递fill的最后一个值。

DF1 %>%
  complete(Datetime = seq(min(Datetime),'hour'),fill = list(X = DF1$X[nrow(DF1)]))