R日期序列-每个月的前n天

R日期序列-每个月的前n天,r,R,我想生成一个日期序列。我可以用它来做 seq(from=as.POSIXct("2011-01-01 0:00", tz="UTC"), to=as.POSIXct("2012-12-19 23:00", tz="UTC"), by="hour") 现在我想要一个序列,只包含每个月的前几天。如何在R中执行此操作?试试看 n <- 1:10 indx <- as.numeric(format(dt, "%d")) %in% n dtSub <- dt[indx]

我想生成一个日期序列。我可以用它来做

seq(from=as.POSIXct("2011-01-01 0:00", tz="UTC"),
    to=as.POSIXct("2012-12-19 23:00", tz="UTC"),
    by="hour")
现在我想要一个序列,只包含每个月的前几天。如何在R中执行此操作?

试试看

n <- 1:10
indx <- as.numeric(format(dt, "%d")) %in% n
dtSub <- dt[indx]
n
f1 <- function(x, n){
n1 <- seq(n)
indx <- as.numeric(format(x, "%d")) %in% n1
x[indx]
}

res <- f1(dt, 10)
max(as.numeric(format(res, "%d")))
#[1] 10
dt <- seq(from=as.POSIXct("2011-01-01 0:00", tz="UTC"),
to=as.POSIXct("2012-12-19 23:00", tz="UTC"),
by="hour")