R 重采样
library(hyfo)
library(lubridate)
# Daily to monthly 降采样
TS <- data.frame(Date = seq(ymd('1999-01-01'), length = 365, by = '1 day'),
num=runif(365, 3, 10))
TS_new <- resample(TS, method = 'day2mon')
# Monthly to daily 升采样
TS <- data.frame(Date = seq(ymd('1999-01-01'), length = 12, by = '1 month'),
num=runif(12, 3, 10))
TS_new <- resample(TS, method = 'mon2day')
library(dplyr)
library(lubridate)
set.seed(2017)
options(digits=4)
expenses <- tibble(
date=seq(ymd("2019-01-01"), ymd("2020-12-31"), by=1),
amount=rgamma(length(date), shape = 2, scale = 20))
expenses %>% group_by(month=floor_date(date, "month")) %>%
summarize(amount=sum(amount))
expenses %>% group_by(month=floor_date(date, "3month")) %>%
summarize(amount=sum(amount))
expenses %>% group_by(month=floor_date(date, "year")) %>%
summarize(amount=sum(amount))
x <- ymd_hms("2009-08-03 12:01:59.23")
#向下
floor_date(expenses$date,'month')
floor_date(expenses$date,'year')
floor_date(expenses$date, "3month")
floor_date(x, ".1s")
floor_date(x, "second")
floor_date(x, "minute")
floor_date(x, "hour")
floor_date(x, "day")
floor_date(x, "week")
floor_date(x, "month")
floor_date(x, "bimonth")
floor_date(x, "quarter")
floor_date(x, "season")
floor_date(x, "halfyear")
floor_date(x, "year")
#向上
ceiling_date(expenses$date,'year')
#取整
round_date(expenses$date, "season")
round_date(expenses$date, "quarter")
library(lubridate)
# Daily to monthly 降采样
TS <- data.frame(Date = seq(ymd('1999-01-01'), length = 365, by = '1 day'),
num=runif(365, 3, 10))
TS_new <- resample(TS, method = 'day2mon')
# Monthly to daily 升采样
TS <- data.frame(Date = seq(ymd('1999-01-01'), length = 12, by = '1 month'),
num=runif(12, 3, 10))
TS_new <- resample(TS, method = 'mon2day')
library(dplyr)
library(lubridate)
set.seed(2017)
options(digits=4)
expenses <- tibble(
date=seq(ymd("2019-01-01"), ymd("2020-12-31"), by=1),
amount=rgamma(length(date), shape = 2, scale = 20))
expenses %>% group_by(month=floor_date(date, "month")) %>%
summarize(amount=sum(amount))
expenses %>% group_by(month=floor_date(date, "3month")) %>%
summarize(amount=sum(amount))
expenses %>% group_by(month=floor_date(date, "year")) %>%
summarize(amount=sum(amount))
x <- ymd_hms("2009-08-03 12:01:59.23")
#向下
floor_date(expenses$date,'month')
floor_date(expenses$date,'year')
floor_date(expenses$date, "3month")
floor_date(x, ".1s")
floor_date(x, "second")
floor_date(x, "minute")
floor_date(x, "hour")
floor_date(x, "day")
floor_date(x, "week")
floor_date(x, "month")
floor_date(x, "bimonth")
floor_date(x, "quarter")
floor_date(x, "season")
floor_date(x, "halfyear")
floor_date(x, "year")
#向上
ceiling_date(expenses$date,'year')
#取整
round_date(expenses$date, "season")
round_date(expenses$date, "quarter")
评论
发表评论