R: 每列中数值的滚动比率

huangapple go评论79阅读模式
英文:

R: rolling ratio of values in each colunm

问题

I can provide the code part of your request in Chinese. Here's the translated code:

# 我想要计算每列中值的滚动比率,例如 5th/1st,6th/2nd 等,并将结果存储在一个新的数据框中。
df <- data.frame(Group = rep(c('A', 'B', 'C', 'D'), each = 4),
                 score_1 = 1:16,
                 score_2 = 5:20)

# 使用上述示例数据,我希望对每一行(n)(从第 5 行开始)进行除以 n-4 的操作,这将创建以下结果表格。

Please note that the code is provided as-is, and you'll need to implement the logic to calculate the rolling ratios yourself.

英文:

I'm looking to get the rolling ratio of values in each column. e.g. 5th/1st, 6th/2nd etc and have the results in a new data frame.

df &lt;- data.frame(Group = rep(c(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;), each = 4),
                 score_1 = 1:16,
                 score_2 = 5:20)        

With the example data above I want each row (n) (from the 5th down) to be divided by n-4, in this case would create the below table of results.

5.0	 1.8
3.0	 1.7
2.3	 1.6
2.0	 1.5
1.8	 1.4
1.7	 1.4
1.6	 1.4
1.5	 1.3
1.4	 1.3
1.4	 1.3
1.4	 1.3
1.3	 1.3

答案1

得分: 1

你可以使用head()tail()

tail(df, -4)[-1] / head(df, -4)[-1]

也可以使用

((x, n) tail(x, n) / head(x, n))(df[-1], -4)

score_1 score_2

5 5.000000 1.800000

6 3.000000 1.666667

7 2.333333 1.571429

8 2.000000 1.500000

9 1.800000 1.444444

10 1.666667 1.400000

11 1.571429 1.363636

12 1.500000 1.333333

13 1.444444 1.307692

14 1.400000 1.285714

15 1.363636 1.266667

16 1.333333 1.250000


---

使用`dplyr`,你可以使用`lag()`:

```r
library(dplyr)

df %>%
  mutate(across(-Group, ~ .x / lag(.x, 4L)))
英文:

You can use head() and tail():

tail(df, -4)[-1] / head(df, -4)[-1]

# Also
# (\(x, n) tail(x, n) / head(x, n))(df[-1], -4)
#
#     score_1  score_2
# 5  5.000000 1.800000
# 6  3.000000 1.666667
# 7  2.333333 1.571429
# 8  2.000000 1.500000
# 9  1.800000 1.444444
# 10 1.666667 1.400000
# 11 1.571429 1.363636
# 12 1.500000 1.333333
# 13 1.444444 1.307692
# 14 1.400000 1.285714
# 15 1.363636 1.266667
# 16 1.333333 1.250000

With dplyr, you can use lag():

library(dplyr)

df %&gt;%
  mutate(across(-Group, ~ .x / lag(.x, 4L)))

答案2

得分: 0

你可以按照以下方式将每个原始向量除以它的滞后版本:

x &lt;- 1:16
x / dplyr::lag(x, n = 4L)
#&gt;  [1]       NA       NA       NA       NA 5.000000 3.000000 2.333333 2.000000
#&gt;  [9] 1.800000 1.666667 1.571429 1.500000 1.444444 1.400000 1.363636 1.333333

创建于2023年03月09日,使用 reprex v2.0.2

英文:

You can divide each original vector with its lagged version like so:

x &lt;- 1:16
x / dplyr::lag(x, n = 4L)
#&gt;  [1]       NA       NA       NA       NA 5.000000 3.000000 2.333333 2.000000
#&gt;  [9] 1.800000 1.666667 1.571429 1.500000 1.444444 1.400000 1.363636 1.333333

<sup>Created on 2023-03-09 with reprex v2.0.2</sup>

答案3

得分: 0

你可以直接用它的子集来划分数据框。

df[5:nrow(df), 2:3]/df[(5:nrow(df)) - 4, 2:3]

    score_1  score_2
5  5.000000 1.800000
6  3.000000 1.666667
7  2.333333 1.571429
8  2.000000 1.500000
9  1.800000 1.444444
10 1.666667 1.400000
11 1.571429 1.363636
12 1.500000 1.333333
13 1.444444 1.307692
14 1.400000 1.285714
15 1.363636 1.266667
16 1.333333 1.250000
英文:

You can directly divide the dataframe with the subset of it.

df[5:nrow(df), 2:3]/df[(5:nrow(df)) - 4, 2:3]

    score_1  score_2
5  5.000000 1.800000
6  3.000000 1.666667
7  2.333333 1.571429
8  2.000000 1.500000
9  1.800000 1.444444
10 1.666667 1.400000
11 1.571429 1.363636
12 1.500000 1.333333
13 1.444444 1.307692
14 1.400000 1.285714
15 1.363636 1.266667
16 1.333333 1.250000

huangapple
  • 本文由 发表于 2023年3月9日 21:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定