1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library("zoo")
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
>
> x <- cbind(
+ c(1, NA, 2, NA, NA, NA, NA, 3),
+ c(NA, 1, NA, 2, NA, NA, NA, 3)
+ )
> na.locf(x)
[,1] [,2]
[1,] 1 NA
[2,] 1 1
[3,] 2 1
[4,] 2 2
[5,] 2 2
[6,] 2 2
[7,] 2 2
[8,] 3 3
> na.locf(x, fromLast = TRUE)
[,1] [,2]
[1,] 1 1
[2,] 2 1
[3,] 2 2
[4,] 3 2
[5,] 3 3
[6,] 3 3
[7,] 3 3
[8,] 3 3
> na.locf(x, maxgap = 3)
[,1] [,2]
[1,] 1 NA
[2,] 1 1
[3,] 2 1
[4,] NA 2
[5,] NA 2
[6,] NA 2
[7,] NA 2
[8,] 3 3
> na.locf(x[,2])
[1] 1 1 2 2 2 2 3
> na.locf(x[,2], na.rm = FALSE)
[1] NA 1 1 2 2 2 2 3
>
> z <- zoo(x, as.Date("2000-01-01") + 0:8)
> na.locf(z)
2000-01-01 1 NA
2000-01-02 1 1
2000-01-03 2 1
2000-01-04 2 2
2000-01-05 2 2
2000-01-06 2 2
2000-01-07 2 2
2000-01-08 3 3
2000-01-09 1 3
> na.locf(z, fromLast = TRUE)
2000-01-01 1 1
2000-01-02 2 1
2000-01-03 2 2
2000-01-04 3 2
2000-01-05 3 3
2000-01-06 3 3
2000-01-07 3 3
2000-01-08 3 3
2000-01-09 1 NA
> na.locf(z, maxgap = 3)
2000-01-01 1 NA
2000-01-02 1 1
2000-01-03 2 1
2000-01-04 NA 2
2000-01-05 NA 2
2000-01-06 NA 2
2000-01-07 NA 2
2000-01-08 3 3
2000-01-09 1 3
> na.locf(z[,2])
2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07 2000-01-08
1 1 2 2 2 2 3
2000-01-09
3
> na.locf(z[,2], na.rm = FALSE)
2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07
NA 1 1 2 2 2 2
2000-01-08 2000-01-09
3 3
>
> d <- as.Date("2000-01-01") + c(0, NA, 2, NA, NA, NA, NA, 7)
> na.locf(d)
[1] "2000-01-01" "2000-01-01" "2000-01-03" "2000-01-03" "2000-01-03"
[6] "2000-01-03" "2000-01-03" "2000-01-08"
> na.locf(d, fromLast = TRUE)
[1] "2000-01-01" "2000-01-03" "2000-01-03" "2000-01-08" "2000-01-08"
[6] "2000-01-08" "2000-01-08" "2000-01-08"
> na.locf(d, maxgap = 3)
[1] "2000-01-01" "2000-01-01" "2000-01-03" NA NA
[6] NA NA "2000-01-08"
>
> proc.time()
user system elapsed
0.196 0.024 0.216
|