2015-08-08 182 views
0

我有從15-01-01到2015-01-01的15年利率日間數據。 我想將這些數據轉換爲月數據,只有月份和年份。如何使用R將每日(每日)數據轉換爲每月數據?

我想表達一個月中所有日子的價值,並把它作爲那個月的一個價值。 我怎樣才能做到這一點R.

> str(mibid) 
'data.frame': 4263 obs. of 6 variables: 
$ Days: int 1 2 3 4 5 6 7 8 9 10 ... 
$ Date: Date, format: "2000-01-03" "2000-01-04" "2000-01-05" "2000-01-06" ... 
$ BID : num 8.82 8.82 8.88 8.79 8.78 8.8 8.81 8.82 8.86 8.78 ... 
$ I.S : num 0.092 0.0819 0.0779 0.0801 0.074 0.0766 0.0628 0.0887 0.0759 0.073 ... 
$ BOR : num 9.46 9.5 9.52 9.36 9.33 9.37 9.42 9.39 9.4 9.33 ... 
$ R.S : num 0.0822 0.0817 0.0828 0.0732 0.084 0.0919 0.0757 0.0725 0.0719 0.0564 ... 
> head(mibid) 
    Days  Date BID I.S BOR R.S 
1 1 2000-01-03 8.82 0.0920 9.46 0.0822 
2 2 2000-01-04 8.82 0.0819 9.50 0.0817 
3 3 2000-01-05 8.88 0.0779 9.52 0.0828 
4 4 2000-01-06 8.79 0.0801 9.36 0.0732 
5 5 2000-01-07 8.78 0.0740 9.33 0.0840 
6 6 2000-01-08 8.80 0.0766 9.37 0.0919 
> 

回答

2

我應該這樣做與XTS:

set.seed(21) 
mibid <- data.frame(Date=Sys.Date()-100:1, 
    BID=rnorm(100, 8, 0.1), I.S=rnorm(100, 0.08, 0.01), 
    BOR=rnorm(100, 9, 0.1), R.S=rnorm(100, 0.08, 0.01)) 
require(xts) 
# convert to xts 
xmibid <- xts(mibid[,-1], mibid[,1]) 
# aggregate 
agg_xmibid <- apply.monthly(xmibid, colMeans) 
# convert back to data.frame 
agg_mibid <- data.frame(Date=index(agg_xmibid), agg_xmibid, row.names=NULL) 
head(agg_mibid) 
#   Date  BID  I.S  BOR  R.S 
# 1 2015-04-30 8.079301 0.07189111 9.074807 0.06819096 
# 2 2015-05-31 7.987479 0.07888328 8.999055 0.08090253 
# 3 2015-06-30 8.043845 0.07885779 9.018338 0.07847999 
# 4 2015-07-31 7.990822 0.07799489 8.980492 0.08162038 
# 5 2015-08-07 8.000414 0.08535749 9.044867 0.07755017 
2

如何這可能會使用dplyrlubridate做一個小例子

set.seed(321) 
dat <- data.frame(day=seq.Date(as.Date("2010-01-01"), length.out=200, by="day"), 
     x = rnorm(200), 
     y = rexp(200)) 
head(dat) 

     day   x   y 
1 2010-01-01 1.7049032 2.6286754 
2 2010-01-02 -0.7120386 0.3916089 
3 2010-01-03 -0.2779849 0.1815379 
4 2010-01-04 -0.1196490 0.1234461 
5 2010-01-05 -0.1239606 2.2237404 
6 2010-01-06 0.2681838 0.3217511 

require(dplyr) 
require(lubridate) 
dat %>% 
    mutate(year = year(day), 
     monthnum = month(day), 
     month = month(day, label=T)) %>% 
    group_by(year, month) %>% 
    arrange(year, monthnum) %>% 
    select(-monthnum) %>% 
    summarise(x = mean(x), 
      y = mean(y)) 

Source: local data frame [7 x 4] 
Groups: year 

    year month   x   y 
1 2010 Jan 0.02958633 0.9387509 
2 2010 Feb 0.07711820 1.0985411 
3 2010 Mar -0.06429982 1.2395438 
4 2010 Apr -0.01787658 1.3627864 
5 2010 May 0.19131861 1.1802712 
6 2010 Jun -0.04894075 0.8224855 
7 2010 Jul -0.22410057 1.1749863 
2

另一種選擇是使用data.table,它有幾個非常方便的日期時間功能。使用@SamThomas的數據:

library(data.table) 
setDT(dat)[, lapply(.SD, mean), by=.(year(day), month(day))] 

這給:

year month   x   y 
1: 2010  1 0.02958633 0.9387509 
2: 2010  2 0.07711820 1.0985411 
3: 2010  3 -0.06429982 1.2395438 
4: 2010  4 -0.01787658 1.3627864 
5: 2010  5 0.19131861 1.1802712 
6: 2010  6 -0.04894075 0.8224855 
7: 2010  7 -0.22410057 1.1749863 

在@JoshuaUlrich的數據:

setDT(mibid)[, lapply(.SD, mean), by=.(year(Date), month(Date))] 

給出:

year month  BID  I.S  BOR  R.S 
1: 2015  5 7.997178 0.07794925 8.999625 0.08062426 
2: 2015  6 8.034805 0.07940600 9.019823 0.07823314 
3: 2015  7 7.989371 0.07822263 8.996015 0.08195401 
4: 2015  8 8.010541 0.08364351 8.982793 0.07748399 

如果你想月份的名稱而不是num別爾斯,你將不得不包括[, day:=as.IDate(day)]setDT()部分之後,用months代替month

setDT(mibid)[, Date:=as.IDate(Date)][, lapply(.SD, mean), by=.(year(Date), months(Date))] 

:尤其是針對大數據集,data.table可能會是(很多)快於其他兩種方案。

+0

非常有用,謝謝! –

相關問題