简体   繁体   中英

How to create several variables in a for loop increasing each one by 3 months in R?

I need to create 12 variables starting from a certain date t1, increasing each one by 3 months.

Here's an example:

 t1           t2           t3     
01-01-2000  01-04-2000   01-07-2000

I've tried something like this (with package lubridate ):

for (i in 1:12) {
     month(AAA$t(i+1)) <- month(AAA$t(i)) + 3 }

But I'm obtaining:

Error in as.POSIXlt(x) : attempt to apply non-function

If you want to create a dataframe with t1 through t12 containing a range of dates:

t = seq(mdy("01/01/2000"), by = "3 months", length.out = 12) #this replaces the loop
names(t) <- paste0("t", c(1:12)) #this names your vector
data.frame(as.list(t)) #this creates the df

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