0

我有一个 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 代替零以避免除以零。)

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

author  startTrendWeek endTrendWeek
author2 1              3
author3 2              4

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

4

1 回答 1

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
于 2012-07-10T16:56:16.280 回答