Reputation: 267
I need to do segmentation of a dataframe based on some conditions, say mean()
and max()
.
here is my dataframe df with one variable A
.
A
0
0
3
2
4
3
...
I need to create two more columns mean
and max
for each row of this column,
Expected Output
A mean max
0 0 0
0 0(mean(A[1], A[2])) 0(max(A[1], A[2]))
3 1(mean(A[1],A[2],A[3])) 3(max(A[1],A[2],A[3]))
2 1.25(mean(A[1],A[2],A[3],A[4])) 3(max(A[1],A[2],A[3],A[4]))
4 1.8(mean(A[1],A[2],A[3],A[4],A[5])) 4(max(A[1],A[2],A[3],A[4],A[5]))
3 2(mean(A[1],A[2],A[3],A[4],A[5],A[6])) 4(max(A[1],A[2],A[3],A[4],A[5],A[6]))
... ... ...
What could be the optimal way to perform this in R?
Upvotes: 1
Views: 46
Reputation: 1904
dplyr
package has many cumulative functions built in. Here's an example.
> library(dplyr)
> btest <- data.frame(a = c(0,0,3,4,2,3), b = rnorm(6))
> btest %>% mutate(mean = cummean(a), max = cummax(a))
a b mean max
1 0 -1.6028412 0.00 0
2 0 0.1953723 0.00 0
3 3 0.2022246 1.00 3
4 4 -0.2744182 1.75 4
5 2 0.3343044 1.80 4
6 3 0.2081762 2.00 4
Upvotes: 3