我有这个银行面板数据,按月显示。我想使用给定季度中三个月的平均值转换为季度频率。以下是我的数据框的head():
db %>% select(Data,Bank,PC) %>% head()
# A tibble: 6 x 3
Data Bank PC
<date> <chr> <dbl>
1 2017-01-01 BANCO BM&FBOVESPA 502630099.
2 2017-01-01 BANCO BONSUCESSO S.A. 1716340938.
3 2017-01-01 BANCO BRADESCARD 5334785523.
4 2017-01-01 BANCO BTG PACTUAL S.A. 98935990703.
5 2017-01-01 BANCO CBSS 596039238.
6 2017-01-01 BANCO CIFRA 116806412.
此数据集包含100多家银行的2017年每月数据。我需要将每个银行系列转换为季度频率。因此,在上面的示例中,我需要为样本中的每个银行计算每季度的PC平均值。换句话说,我需要将面板数据的频率从每月更改为每季度一次。如何在R中完成数据?
以下是我的数据子集,如果有人想尝试的话。
structure(list(Data = structure(c(17167, 17198, 17226, 17257,
17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501), class = "Date"),
Bank = c("BANCO ORIGINAL", "BANCO ORIGINAL", "BANCO ORIGINAL",
"BANCO ORIGINAL", "BANCO ORIGINAL", "BANCO ORIGINAL", "BANCO ORIGINAL",
"BANCO ORIGINAL", "BANCO ORIGINAL", "BANCO ORIGINAL", "BANCO ORIGINAL",
"BANCO ORIGINAL"), Taxonomy = c("BANCOS MULTIPLOS", "BANCOS MULTIPLOS",
"BANCOS MULTIPLOS", "BANCOS MULTIPLOS", "BANCOS MULTIPLOS",
"BANCOS MULTIPLOS", "BANCOS MULTIPLOS", "BANCOS MULTIPLOS",
"BANCOS MULTIPLOS", "BANCOS MULTIPLOS", "BANCOS MULTIPLOS",
"BANCOS MULTIPLOS"), Liq = c(1.1997748808169, 1.19259719096416,
1.17984955128666, 1.18695199874656, 1.19834389025218, 1.22404552882798,
1.24812868632381, 1.25155515564228, 1.23908341171122, 1.22923764062342,
1.22573014109315, 1.19451856437255), Cap = c(0.0564130001266887,
0.0511332310836733, 0.055115912263737, 0.0610766393321285,
0.0516610760871821, 0.0519465686269887, 0.0582801135631064,
0.053508898053011, 0.0557890616137837, 0.0639122025397535,
0.0588865316259913, 0.0534625111456079), Size = c(24.3393816873135,
24.4387493714898, 24.3615598295339, 24.2589048360285, 24.4251409240454,
24.4154251928763, 24.3141208787945, 24.4120044047441, 24.3647597828241,
24.219599367394, 24.304650218349, 24.3922876799839), risk = c(0.220292714085706,
0.226351022665361, 0.225513481661864, 0.261226698162742,
0.268547230949181, 0.193220911345295, 0.189681335841555,
0.203642574652873, 0.17111480849187, 0.198715216005694, 0.189870257131344,
0.13419221010663), profitability = c(-0.01753800950183, -0.0175186818010891,
-0.017557013896287, -0.0175563818325023, -0.0175772644337278,
-0.0176513268874473, -0.0161626726677722, -0.015962366730429,
-0.0160506385110374, -0.0161994381957536, -0.0161484427069834,
-0.0162943410707466)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
答案 0 :(得分:1)
您可以将月份值除以3(并加1以将qtr
转换为1:4)。如果数据跨越多年,则可能要粘贴年份值:
aggregate(dat[4:8], list( qtr=as.POSIXlt(dat$Data)$mon%/%3 +1, bank=dat$Bank), mean)
qtr bank Liq Cap Size risk profitability
1 1 BANCO ORIGINAL 1.190741 0.05422071 24.37990 0.2240524 -0.01753790
2 2 BANCO ORIGINAL 1.203114 0.05489476 24.36649 0.2409983 -0.01759499
3 3 BANCO ORIGINAL 1.246256 0.05585936 24.36363 0.1881462 -0.01605856
4 4 BANCO ORIGINAL 1.216495 0.05875375 24.30551 0.1742592 -0.01621407
结果发现动物园里有一个yearqtr班,甚至会更好:
library(zoo)
aggregate(dat[4:8], list( qtr=as.yearqtr(dat$Data), bank=dat$Bank), mean)
qtr bank Liq Cap Size risk profitability
1 2017 Q1 BANCO ORIGINAL 1.190741 0.05422071 24.37990 0.2240524 -0.01753790
2 2017 Q2 BANCO ORIGINAL 1.203114 0.05489476 24.36649 0.2409983 -0.01759499
3 2017 Q3 BANCO ORIGINAL 1.246256 0.05585936 24.36363 0.1881462 -0.01605856
4 2017 Q4 BANCO ORIGINAL 1.216495 0.05875375 24.30551 0.1742592 -0.01621407
答案 1 :(得分:0)
如果您使用库lubridate
,则可以生成季度:
library(lubridate)
db %>% mutate(qData = quarter(Data))
最好!