sequential subtraction in r
r, seq, subtraction
Solution
You are looking for `diff()`. See its help page by typing `?diff`. Here are the indices of days that fulfill your criterion:
> value <- c(21,23.4,10.7,5.6,3.2,35.2,12.9,67.8)
> which(diff(value)/head(value,-1)>0.5)+1
[1] 6 8
Problem
I would highly appreciate if somebody could help me out with this. This looks simple but I have no clue how to go about it. I am trying to work out the percentage change in one row with respect to the previous one. For example: my data frame looks like this: ``` day value 1 21 2 23.4 3 10.7 4 5.6 5 3.2 6 35.2 7 12.9 8 67.8 . . . . . . 365 27.2 ``` What I am trying to do is to calculate the percentage change in each row with respect to previous row. For example: ``` day value 1 21 2 (day2-day1/day1)*100 3 (day3-day2/day2)*100 4 (day4-day3/day3)*100 5 (day5-day4/day4)*100 6 (day6-day5/day5)*100 7 (day7-day6/day6)*100 8 (day8-day7/day7)*100 . . . . . . 365 (day365-day364/day364)*100 ``` and then print out only those days where the there was a percentage increase of >50% from the previous row Many thanks