Substitute values (for specific dates) from a second data frame to the first data frame
Posted
by
user1665355
on Stack Overflow
See other posts from Stack Overflow
or by user1665355
Published on 2012-10-21T15:58:41Z
Indexed on
2012/10/21
17:01 UTC
Read the original article
Hit count: 508
r
I have two time series data frames:
The first one:
head(df1) :
GMT
MSCI ACWI DJGlbl Russell 1000 Russell Dev S&P GSCI Industrial S&P GSCI Precious
1999-03-01 -0.7000000 0.2000000 -0.1000000 -1.5000000 -1.0000000 -0.4000000
1999-03-02 -0.5035247 0.0998004 -0.7007007 -0.2030457 0.4040404 -0.3012048
1999-03-03 -0.2024291 0.2991027 0.0000000 -0.6103764 0.1006036 -0.1007049
1999-03-04 0.7099391 0.2982107 1.5120968 -0.1023541 0.5025126 0.4032258
1999-03-05 2.4169184 0.8919722 2.1847071 2.7663934 -1.2000000 0.0000000
1999-03-08 0.3933137 0.3929273 0.5830904 -0.0997009 -0.2024291 1.1044177
tail(df1) :
GMT
MSCI ACWI DJGlbl Russell 1000 Russell Dev S&P GSCI Industrial S&P GSCI Precious
2011-12-23 0.68241470 0.84790673 0.9441385 0.6116208 0.5822862 -0.2345300
2011-12-26 -0.05213764 0.00000000 0.0000000 0.0000000 0.0000000 0.0000000
2011-12-27 0.20865936 0.05254861 0.3117693 0.2431611 0.0000000 -0.7233273
2011-12-28 -0.62467465 -1.20798319 -1.1655012 -0.9702850 -2.0414381 -2.4043716
2011-12-29 0.52383447 0.47846890 0.8647799 0.5511329 -0.0933126 -1.2504666
2011-12-30 0.26055237 1.03174603 -0.4676539 1.2180268 1.9613948 1.7388017
The second one:
head(df2) :
GMT MSCI.ACWI DJGlbl Russell.1000 Russell.Dev S.P.GSCI.Industrial S.P.GSCI.Precious
1999-06-01 0.00000000 0.24438520 0.0000000 0 -0.88465521 0.008522842
1999-07-01 0.12630441 0.06755621 0.0000000 0 0.29394697 0.000000000
1999-08-02 0.07441812 0.18922829 0.0000000 0 0.02697299 -0.107155063
1999-09-01 -0.36952701 0.08684107 0.1117509 0 0.24520976 0.000000000
1999-10-01 0.00000000 0.00000000 0.0000000 0 0.00000000 1.941266205
1999-11-01 0.41879925 0.00000000 0.0000000 0 0.00000000 -0.197897901
tail(df2) :
GMT MSCI.ACWI DJGlbl Russell.1000 Russell.Dev S.P.GSCI.Industrial S.P.GSCI.Precious
2011-07-01 0.00000000 0.0000000 0.0000000 0.0000000 0.00000000 -0.1141162
2011-08-01 0.00000000 0.0000000 0.0000000 0.0000000 0.02627347 0.0000000
2011-09-01 -0.02470873 0.2977585 -0.0911891 0.6367605 0.00000000 0.2830977
2011-10-03 0.42495188 0.0000000 0.4200743 -0.4420027 -0.41012646 0.0000000
2011-11-01 0.00000000 0.0000000 0.0000000 -0.6597739 0.00000000 0.0000000
2011-12-01 0.50273034 0.0000000 0.0000000 0.6476393 0.00000000 0.0000000
The first df cointains daily observations. The second df contains only the "first day of each month" forecasted values. I would like to substitute the values from the second df into the first one. In other words, the "first day of each month" values in the first df will be substituted for the "first day of each month" values from the second df. I tried to write an lapply loop that substitutes the values and was only trying to use match function. But I failed. I could not find the similar question at StackOverflow either...
Greatful for any suggestions!
© Stack Overflow or respective owner