[R] Query about calculating the monthly average of daily data columns
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Mon Oct 21 05:15:45 CEST 2019
Hi Subhamitra,
This is not the only way to do this, but if you only want the monthly
averages, it is simple:
# I had to change the "soft" tabs in your email to commas
# in order to read the data in
spdf<-read.table(text="PERMNO,DATE,Spread
111,19940103,0.025464308
111,19940104,0.064424296
111,19940105,0.018579337
111,19940106,0.018872211
111,19940107,0.065279782
111,19940110,0.063485905
111,19940111,0.018355453
111,19940112,0.064135683
111,19940113,0.063519987
111,19940114,0.018277351
111,19940117,0.018628417
111,19940118,0.065630229
111,19940119,0.018713152
111,19940120,0.019119037
111,19940121,0.068342043
111,19940124,0.020843244
111,19940125,0.019954211
111,19940126,0.018980321
111,19940127,0.066827165
111,19940128,0.067459235
111,19940131,0.068682559
111,19940201,0.02081465
111,19940202,0.068236091
111,19940203,0.068821406
111,19940204,0.020075648
111,19940207,0.066070584
111,19940208,0.066068837
111,19940209,0.019077072
111,19940210,0.065894875
111,19940211,0.018847478
111,19940214,0.065040844
111,19940215,0.01880332
111,19940216,0.018836199
111,19940217,0.066888865
111,19940218,0.067116793
111,19940221,0.068809742
111,19940222,0.068230213
111,19940223,0.069502855
111,19940224,0.070383523
111,19940225,0.020430811
111,19940228,0.067087257
111,19940301,0.066776479
111,19940302,0.019959031
111,19940303,0.066596469
111,19940304,0.019131334
111,19940307,0.019312528
111,19940308,0.067349909
111,19940309,0.068916431
111,19940310,0.068620043
111,19940311,0.070494844
111,19940314,0.071056842
111,19940315,0.071042517
111,19940316,0.072401771
111,19940317,0.071940001
111,19940318,0.07352884
111,19940321,0.072671688
111,19940322,0.072652595
111,19940323,0.021352138
111,19940324,0.069933727
111,19940325,0.068717467
111,19940328,0.020470748
111,19940329,0.020003748
111,19940330,0.065833717
111,19940331,0.065268388
111,19940401,0.018762356
111,19940404,0.064914179
111,19940405,0.064706743
111,19940406,0.018764175
111,19940407,0.06524806
111,19940408,0.018593449
111,19940411,0.064913949
111,19940412,0.01872089
111,19940413,0.018729328
111,19940414,0.018978773
111,19940415,0.065477137
111,19940418,0.064614365
111,19940419,0.064184148
111,19940420,0.018553192
111,19940421,0.066872771
111,19940422,0.06680782
111,19940425,0.067467961
111,19940426,0.02014297
111,19940427,0.062464016
111,19940428,0.062357052
112,19940429,0.000233993
112,19940103,0.000815264
112,19940104,0.000238165
112,19940105,0.000813632
112,19940106,0.000236915
112,19940107,0.000809102
112,19940110,0.000801642
112,19940111,0.000797932
112,19940112,0.000795251
112,19940113,0.000795186
112,19940114,0.000231359
112,19940117,0.000232134
112,19940118,0.000233718
112,19940119,0.000233993
112,19940120,0.000234694
112,19940121,0.000235753
112,19940124,0.000808653
112,19940125,0.000235604
112,19940126,0.000805068
112,19940127,0.000802337
112,19940128,0.000801768
112,19940131,0.000233517
112,19940201,0.000797431
112,19940202,0.000233338
112,19940203,0.000233826
112,19940204,0.000799519
112,19940207,0.000798105
112,19940208,0.000792245
112,19940209,0.000231113
112,19940210,0.000233413
112,19940211,0.000798168
112,19940214,0.000233282
112,19940215,0.000797848
112,19940216,0.000785165
112,19940217,0.000228426
112,19940218,0.000786783
112,19940221,0.00078343
112,19940222,0.000781459
112,19940223,0.000776264
112,19940224,0.000226399
112,19940225,0.000779066
112,19940228,0.000773603
112,19940301,0.000226487
112,19940302,0.000775233
112,19940303,0.000227017
112,19940304,0.000227854
112,19940307,0.000782814
112,19940308,0.000229164
112,19940309,0.000787033
112,19940310,0.000784049
112,19940311,0.000228984
112,19940314,0.00078697
112,19940315,0.000782567
112,19940316,0.000228516
112,19940317,0.000786347
112,19940318,0.000229236
112,19940321,0.000230107
112,19940322,0.000792689
112,19940323,0.000787284
112,19940324,0.000787221
112,19940325,0.000227978",
header=TRUE,sep=",",stringsAsFactors=FALSE)
# split the year and month out of the date string
# as you have more than one year in your complete
# data set
spdf$yrmon<-substr(spdf$DATE,1,6)
# get the mean for each PERMNO and year/month
by(spdf$Spread,spdf[,c("PERMNO","yrmon")],mean)
Jim
On Sun, Oct 20, 2019 at 11:09 PM Subhamitra Patra <
subhamitra.patra using gmail.com> wrote:
>
> Here, I am asking one more query (just for learning purpose) that if my
> country name and its respective variable is in the panel format, and I want
> to take the monthly average for each country, how the code will be
> arranged. For your convenience, I am providing a small data sample below.
>
>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list