merge - r zoo object: different fill for different columns -
merge - r zoo object: different fill for different columns -
i have 2 zoo objects in r, a1 , a2, want merge, fill in missing values. want fill missing values of a1 na.approx()
, , want fill missing values of a2 na.locf()
. how can accomplish this?
an example:
a1 <- zoo(data.frame(b=c(1,3,4,6),c=c(2,6,8,12)),c(1,3,4,6))
b c 1 1 2 3 3 6 4 4 8 6 6 12
a2 <- zoo(data.frame(d=c(1,0)),c(2,5))
d 2 1 5 0
a3 <- merge(a1,a2)
b c d 1 1 2 na 2 na na 1 3 3 6 na 4 4 8 na 5 na na 0 6 6 12 na
now a4:
b c d 1 1 2 na 2 2 4 1 3 3 6 1 4 4 8 1 5 5 10 0 6 6 12 0
but a4 <- na.locf(a3)
or a4 <- na.approx(a3)
both treat columns same. how can treat columns separately? or maybe can prepare prior merging?
thanks in advance
**edit: includes real info **
to illustrate in more detail, here's real data:
> dput(a5) structure(c(133.7, na, 133.345, na, 134.2, na, 135.721, 136.456, 136.677, 137.347, 138.324, na, 139.086, 139.622, 140.475, na, 141.179, 141.652, 141.811, 125.901, na, 125.965, na, 127.402, na, 128.529, 128.797, 129.267, 130.08, 130.831, na, 131.313, 132.008, 132.85, na, 133.416, 133.842, 133.986, na, 0, na, 1, na, 0, na, na, na, na, na, 1, na, na, na, 0, na, na, na), .dim = c(19l, 3l), .dimnames = list(null, c("sup", "ret", "gas")), index = structure(c(1387242143, 1387242156, 1387242158, 1387242169, 1387242173, 1387242186, 1387242188, 1387242203, 1387242218, 1387242233, 1387242248, 1387242252, 1387242263, 1387242278, 1387242293, 1387242305, 1387242308, 1387242323, 1387242338 ), class = c("posixct", "posixt")), class = "zoo") > a5 sup ret gas 2013-12-16 19:02:23 133.700 125.901 na 2013-12-16 19:02:36 na na 0 2013-12-16 19:02:38 133.345 125.965 na 2013-12-16 19:02:49 na na 1 2013-12-16 19:02:53 134.200 127.402 na 2013-12-16 19:03:06 na na 0 2013-12-16 19:03:08 135.721 128.529 na 2013-12-16 19:03:23 136.456 128.797 na 2013-12-16 19:03:38 136.677 129.267 na 2013-12-16 19:03:53 137.347 130.080 na 2013-12-16 19:04:08 138.324 130.831 na 2013-12-16 19:04:12 na na 1 2013-12-16 19:04:23 139.086 131.313 na 2013-12-16 19:04:38 139.622 132.008 na 2013-12-16 19:04:53 140.475 132.850 na 2013-12-16 19:05:05 na na 0 2013-12-16 19:05:08 141.179 133.416 na 2013-12-16 19:05:23 141.652 133.842 na 2013-12-16 19:05:38 141.811 133.986 na
stu's solution doesn't preserve accuracy of 1st 2 columns of data. na's in first 2 columns interpolated na.approx()
, , lastly column filled last-observation-carried-forward locf()
.
> na.locf(ceiling(na.approx(a5))) sup ret gas 2013-12-16 19:02:23 134 126 na 2013-12-16 19:02:36 134 126 0 2013-12-16 19:02:38 134 126 1 2013-12-16 19:02:49 134 128 1 2013-12-16 19:02:53 135 128 1 2013-12-16 19:03:06 136 129 0 2013-12-16 19:03:08 136 129 1 2013-12-16 19:03:23 137 129 1 2013-12-16 19:03:38 137 130 1 2013-12-16 19:03:53 138 131 1 2013-12-16 19:04:08 139 131 1 2013-12-16 19:04:12 139 131 1 2013-12-16 19:04:23 140 132 1 2013-12-16 19:04:38 140 133 1 2013-12-16 19:04:53 141 133 1 2013-12-16 19:05:05 142 134 0 2013-12-16 19:05:08 142 134 0 2013-12-16 19:05:23 142 134 0 2013-12-16 19:05:38 142 134 0
** above not need **
thanks again
** edit - showing solution result looking **
> a6 <- cbind(na.approx(a5[,c("sup","ret")]),na.locf(a5[,c("gas")])) > a6 sup ret na.locf(a5[, c("gas")]) 2013-12-16 19:02:23 133.7000 125.9010 na 2013-12-16 19:02:36 133.3923 125.9565 0 2013-12-16 19:02:38 133.3450 125.9650 0 2013-12-16 19:02:49 133.9720 127.0188 1 2013-12-16 19:02:53 134.2000 127.4020 1 2013-12-16 19:03:06 135.5182 128.3787 0 2013-12-16 19:03:08 135.7210 128.5290 0 2013-12-16 19:03:23 136.4560 128.7970 0 2013-12-16 19:03:38 136.6770 129.2670 0 2013-12-16 19:03:53 137.3470 130.0800 0 2013-12-16 19:04:08 138.3240 130.8310 0 2013-12-16 19:04:12 138.5272 130.9595 1 2013-12-16 19:04:23 139.0860 131.3130 1 2013-12-16 19:04:38 139.6220 132.0080 1 2013-12-16 19:04:53 140.4750 132.8500 1 2013-12-16 19:05:05 141.0382 133.3028 0 2013-12-16 19:05:08 141.1790 133.4160 0 2013-12-16 19:05:23 141.6520 133.8420 0 2013-12-16 19:05:38 141.8110 133.9860 0
still have deal names of new object, that's easy one.
use column-bind function while subsetting each column desired na.fill
function:
a4 <- cbind(na.approx(a3[,1:2]), na.locf(a3[,3]))
it's 1 line of code, , can rearrange columns want.
r merge time-series zoo
Comments
Post a Comment