This is quite complicated for me and I would be really grateful if someone could tell me how to go about this problem. My dataframe has two columns:
dat <- structure(list(day = 172:208,
x = c(0.14, 0.02, 0.09, 3.06, 3.21,
4.15, 6.24, 6.27, 3.31, 6.28,
16.9, 20.1, 20.29, 20.45, 17.52,
6.22, 1.14, 0.84, 0.68, 0.49,
0.22, 0.01, 0.01, 0.6, 0.64, 0.64,
0.66, 0.69, 0.15, 0.15, 3.16,
3.44, 3.42, 3.37, 3.51, 2.77, 3.51
)),
.Names = c("day", "x"),
class = "data.frame", row.names = c(NA,-37L))
dat
day x
172 0.14
173 0.02
174 0.09
175 3.06
176 3.21
177 4.15
178 6.24
179 6.27
180 3.31
181 6.28
182 16.90
183 20.10
184 20.29
185 20.45
186 17.52
187 6.22
188 1.14
189 0.84
190 0.68
191 0.49
192 0.22
193 0.01
194 0.01
195 0.60
196 0.64
197 0.64
198 0.66
199 0.69
200 0.15
201 0.15
202 3.16
203 3.44
204 3.42
205 3.37
206 3.51
207 2.77
208 3.51
What I want to do is this:
1) In column x, look for values greater than 2.3
which(x>2.3)
2) For the day where x is greater than 2.3, calculate the percentage change in x for next 3 days. For example, for 175 day, x is 3.06 (>2.3), therefore for next 3 consecutive values of x (3.21 - day 176, 4.15 - day 177, 6.24 - day 178), do this:
(3.21 - 3.06)*100/3.06 = 4.9
(4.15 - 3.21)*100/3.21 = 0.29
(6.24 - 4.15)*100/4.15 = 50.36
and if the all the above three values are greater than -30, then store the middle day from 176, 177 and 178 in a separate vector (in this case, store 177).
3) If the three values are less than -30, then start again from 179 (>2.3 mm) and repeat step 2 for day 180, 181 and 182.
(3.31 - 6.27)*100/6.27 = -47.2
(6. 28 - 3.31)*100/3.31 = 89.72
(16.9 - 6.28) * 100/6.28 = 169.1
If all the values are greater than -30, then store the middle day (181). In this case, one of the values is less than -30, therefore do not store anything and start again fromfrom 183 (>2.3 mm) and repeat again for 184, 185 and 186. If a value out of 3 values above is again less than -30, start from day 187 (x > 2.3) and repeat step 2 for day 188,189 and 190. If again a single value out of three is less than -30, then start from 202 (since for 202, x > 2.3)
I am really sorry I do not have much programming experience here in r therefore posting this question which has bogged me down for quite a time.
Thanks a lot