Based on the dataframe below, I would like to create a new column using rollmean based on three conditions - the values in column b match each other, the minimum values to be averaged in column a is 2, and I only want to average all values below the current row. If the amount of values to average is 2 or less, I would like to return a blank value.
I'm assuming that I will have to use an apply function to do this, but I'm not sure where to start.
a=c(1,2,3,4,1,2,3,4,1,2,3,4)
b=c("X","X","X","X","Y","Y","Y","Y","Z","Z","Z","Z")
df=as.data.frame(cbind(a,b))
I would like the final table to look like:
Name Value Output
X 1 2.5
X 2 3
X 3
X 4
Y 1 2.5
Y 2 3
Y 3
Y 4
Z 1 2.5
Z 2 3
Z 3
Z 4