Reputation: 934
roll = df.rolling(window=3, center=False).quantile(0.5)
I am passing [72,73,74,71,75,72,77,75,72,79,73,72] as a DF
I get min 74
max 77
this code i did in Python
I want to do this in R
there are many libs in R like caTools
and many more
runquantile(data[,2],3,probs = 0.5)
(i tried this)
but no one is working properly
please help me out
Edit.
Based on the OP comment, here are data and code. This works with me.
DF <- c(72, 73, 74, 71, 75, 72, 77, 75, 72, 79, 73, 72)
runquantile(DF, 3, probs = 0.5)
Upvotes: 0
Views: 646
Reputation: 160447
With almost no exception, anything "rolling" to me suggests the zoo
package:
zoo::rollapply(DF, k=3, FUN=median)
# [1] 73 73 74 72 75 75 75 75 73 73
### this returns length(DF)-2*floor(width/2),
### problem if putting into a data.frame .. if so, then pad it:
zoo::rollapply(DF, 3, FUN=median, fill=NA)
# [1] NA 73 73 74 72 75 75 75 75 73 73 NA
(I'll pad all from here on out.)
Quantiles:
zoo::rollapply(DF, width=3, quantile, probs=0.5, fill=NA)
# 50% 50% 50% 50% 50% 50% 50% 50% 50% 50%
# NA 73 73 74 72 75 75 75 75 73 73 NA
Quantiles with multiple probs
returns a matrix:
zoo::rollapply(DF, width=3, quantile, probs=c(0.5,0.6), fill=NA)
# 50% 60%
# [1,] NA NA
# [2,] 73 73.2
# [3,] 73 73.2
# [4,] 74 74.2
# [5,] 72 72.6
# [6,] 75 75.4
# [7,] 75 75.4
# [8,] 75 75.4
# [9,] 75 75.8
# [10,] 73 74.2
# [11,] 73 74.2
# [12,] NA NA
Same thing, but we can write a function (anonymous or named) that does whatever-we-need:
zoo::rollapply(DF, width=3, FUN = function(x) quantile(x, probs=c(0.5,0.6)), fill=NA)
For clarity, your statement "I get min 74 max 77" can be misleading. I get the full data:
In [15]: DF = pd.DataFrame({'B': [72,73,74,71,75,72,77,75,72,79,73,72]})
In [16]: DF.rolling(window=3, center=False).quantile(0.5)
Out[16]:
B
0 NaN
1 NaN
2 73.0
3 73.0
4 74.0
5 72.0
6 75.0
7 75.0
8 75.0
9 75.0
10 73.0
11 73.0
Since center=False
, perhaps the call to zoo::rollapply
above should use align=
:
zoo::rollapply(DF, width=3, quantile, probs=0.5, fill = NA, align="right")
# 50% 50% 50% 50% 50% 50% 50% 50% 50% 50%
# NA NA 73 73 74 72 75 75 75 75 73 73
The 50%
labels are an artifact of using quantile
and can be safely ignored and/or removed, as in
zoo::rollapply(DF, width=3, quantile, probs=0.5, fill = NA, align="right", names=FALSE)
# [1] NA NA 73 73 74 72 75 75 75 75 73 73
Upvotes: 6