数据框中列之间的计算:每周一次的趋势'销售检测

时间:2012-07-10 16:31:06

标签: r calculated-columns trending

我有一个data.frame代表超过25周的一组作者的频率图书销售:

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

首先,我想使用这些数据来构建一个新的数据框,显示[currentWeek / previousWeek]的分数。也许是这样的事情:

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   

(我想用1代替零,以避免除以零。)

其次,我希望对所有行进行快速迭代,检查相邻周的任何三元组,其中作者的销售额在连续两周的两对中增加了两倍,并在某种输出中报告表。也许是这样的:

author  startTrendWeek endTrendWeek
author2 1              3
author3 2              4

关于如何在R中解决这些问题的任何想法?

1 个答案:

答案 0 :(得分:4)

重新创建数据:

x <- read.table(text=
"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
                ", header=TRUE)

一行代码:

cbind(x[1], t(apply(x[, -1], 1, function(xx)xx[-1]/xx[-length(xx)])))

   author    week_2 week_3    week_4
1 author1 0.5714286   1.25 0.4000000
2 author2 2.0000000   3.00 0.2777778
3 author3 0.0000000    Inf 2.0000000
4 author4       Inf   1.00 2.0000000
5 author5       Inf   0.00       NaN