I want to subtract a number of my choice from any given current observation before I apply a function to my data in a dplyr pipe.
For example, let's compute the mean a) based on the real observation and b) when subtracting .10 from the current observation. The solution should be applicable to other computations or functions.
Let's say, we look at ice prices of three different ices (ice_id = ice identifier) at three different days (day).
da <- data.frame(ice_id = c(1,1,1,2,2,2,3,3,3), day = c(1,2,3,1,2,3,1,2,3), price = c(1.60,1.90,1.80,2.10,2.05,2.30,0.50,0.40,0.35))
da
ice_id day price
1 1 1 1.60
2 1 2 1.90
3 1 3 1.80
4 2 1 2.10
5 2 2 2.05
6 2 3 2.30
7 3 1 0.50
8 3 2 0.40
9 3 3 0.35
Now I want to add two columns: 1) Mean ice price at that day based on the real observations of the three ices. 2) Mean ice price at that day if only the ice in the current row would be .10 lower in price (= subtract .10 from the current price observation).
1) is clear to me, but how can I add 2)?
da = da %>%
group_by(day) %>%
mutate(mean_dayprice = mean(price),
mean_dayprice_lower = ?)
For example, in the first row mean_dayprice_lower would be given by:
((1.60-.10)+2.10+.50)/3 = 1.36666