I have a data.frame representing frequency book sales for a set of authors over 25 weeks:
author week_1 week_2 week_3 week_4 ...
author1 7 4 5 2
author2 3 6 18 5
author3 1 0 2 4
author4 0 1 1 2
author5 0 1 0 0
First, I want to use this data to build a new data frame, which shows the fraction of [currentWeek / previousWeek]. Something like this perhaps:
author week_1 week_2 week_3 week_4 ...
author1 NA 0.57 1.25 0.2
author2 NA 2 3 0.28
author3 NA 0 2 2
author4 NA 1 1 2
author5 NA 1 0 0
(I would like to substitute zeros with 1s to avoid dividing by zero.)
Second, I want to run a quick iteration over all the rows, check for any triplets of adjacent weeks where sales for that authors have increased by 100% twice in two consecutive week-pairs, and report this in some kind of output table. Perhaps like this:
author startTrendWeek endTrendWeek
author2 1 3
author3 2 4
Any ideas for how I could solve either of these in R?