time series - Create date index and add to data frame in R -


currently transitioning python r. in python, can create date range pandas , add data frame so;

data = pd.read_csv('data') dates = pd.date_range('2006-01-01 00:00', periods=2920, freq='3h') df = pd.dataframe({'data' : data}, index = dates) 

how can in r?

further, if want compare 2 datasets different lengths same time span, can resample dataset lower frequency can same length higher frequency placing 'nans' in holes so:

df2 = pd.read_csv('data2') #3 hour resolution = 2920 points of data data2 = df2.resample('30min').asfreq() #30 min resolution = 17520 points 

i guess i'm looking pandas package equivalent r. how can code these in r?

the following way of getting time-series data given time interval (3 hours)to (30 minutes):

get data:

starter_df <- data.frame(dates=seq(from=(as.posixct(strftime("2006-01-01 00:00"))),                                length.out = 2920,                                 by="3 hours"),                      data = rnorm(2920)) 

get full sequence in 30 minute intervals , replace na's values starter_df data.frame:

full_data <- data.frame(dates=seq(from=min(starter_df$dates), to=max(starter_df$dates), by="30 min"), data=rep(na,nrow(seq(from=min(starter_df$dates), to=max(starter_df$dates), by="30 min"))))  full_data[full_data$dates %in% starter_df$dates,] <- starter_df[starter_df$dates %in% full_data$dates,] 

i hope helps.


Comments