将列求和,然后除以相邻的单元格。

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

Sum up columns and divide them by adjacent cell

问题

以下是您要翻译的内容:

  1. ## example data d:
  2. d <-
  3. structure(list(Date = c("07.10.2021", "08.10.2021", "09.10.2021",
  4. "10.10.2021", "11.10.2021", "12.10.2021", "01.10.2022"), X1 = c(20L,
  5. 26L, 29L, 20L, 20L, 30L, 39L), X2 = c(25L, 25L, 29L, 29L, 30L,
  6. 30L, NA), X3 = c(25L, 25L, 25L, 25L, 30L, NA, NA), X4 = c(26L,
  7. 26L, 26L, 31L, NA, NA, NA), X5 = c(26L, 25L, 39L, NA, NA, NA,
  8. NA), X6 = c(25L, 31L, NA, NA, NA, NA, NA), X7 = c(31L, NA, NA,
  9. NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 7L))

我理解您要求只提供代码部分的翻译,以下是代码的翻译:

  1. ## 示例数据 d:
  2. d <-
  3. structure(list(Date = c("07.10.2021", "08.10.2021", "09.10.2021",
  4. "10.10.2021", "11.10.2021", "12.10.2021", "01.10.2022"), X1 = c(20L,
  5. 26L, 29L, 20L, 20L, 30L, 39L), X2 = c(25L, 25L, 29L, 29L, 30L,
  6. 30L, NA), X3 = c(25L, 25L, 25L, 25L, 30L, NA, NA), X4 = c(26L,
  7. 26L, 26L, 31L, NA, NA, NA), X5 = c(26L, 25L, 39L, NA, NA, NA,
  8. NA), X6 = c(25L, 31L, NA, NA, NA, NA, NA), X7 = c(31L, NA, NA,
  9. NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 7L))

请注意,我已经忽略了问题中的其他部分,只提供了代码的翻译。

英文:

I have the following table:

Date 1 2 3 4 5 6 7
07/10/2021 20 25 25 26 26 25 31
08/10/2021 26 25 25 26 25 31
09/10/2021 29 29 25 26 39
10/10/2021 20 29 25 31
11/10/2021 20 30 30
12/10/2021 30 30
1/10/2022 39
  1. ## example data d:
  2. d &lt;-
  3. structure(list(Date = c(&quot;07.10.2021&quot;, &quot;08.10.2021&quot;, &quot;09.10.2021&quot;,
  4. &quot;10.10.2021&quot;, &quot;11.10.2021&quot;, &quot;12.10.2021&quot;, &quot;01.10.2022&quot;), X1 = c(20L,
  5. 26L, 29L, 20L, 20L, 30L, 39L), X2 = c(25L, 25L, 29L, 29L, 30L,
  6. 30L, NA), X3 = c(25L, 25L, 25L, 25L, 30L, NA, NA), X4 = c(26L,
  7. 26L, 26L, 31L, NA, NA, NA), X5 = c(26L, 25L, 39L, NA, NA, NA,
  8. NA), X6 = c(25L, 31L, NA, NA, NA, NA, NA), X7 = c(31L, NA, NA,
  9. NA, NA, NA, NA)), class = &quot;data.frame&quot;, row.names = c(NA, 7L))

I'm trying to add up the columns, but only up to the column that has an adjacent cell. I then am trying to divide each sum by the previous sum. For example, column 1 and 2 would add down to 12/10/2021 and then column 2 would be divided by column 1. The output would be the table plus a row with the total values under each of the columns, as shown below:

Date 1 2 3 4 5 6 7
07/10/2021 20 25 25 26 26 25 31
08/10/2021 26 25 25 26 25 31
09/10/2021 29 29 25 26 39
10/10/2021 20 29 25 31
11/10/2021 20 30 30
12/10/2021 30 30
1/10/2022 39
TOTAL 1 1.10 .94 1.09 1.15 1.09 1.24

I tried:

  1. f &lt;- function(.x, .y) {
  2. keep_rows &lt;- cumsum(is.na(.y)) == 0
  3. sum(.x[keep_rows])
  4. }

but that didn't work

答案1

得分: 1

以下是您要翻译的代码部分:

  1. 一个方法:

(d[,8:3] |> apply(2, cumsum)/
d[,7:2] |> apply(2, cumsum)
) |>
diag() |>
rev() |>
setNames(nm = names(d)[-c(1:2)])

  1. 结果:
  2. ```R
  3. ## X2 X3 X4 X5 X6 X7
  4. ## 1.158621 0.942029 1.090000 1.153846 1.098039 1.240000

其中 d 是您的数据框架:

  1. d &lt;-
  2. structure(list(Date = c(&quot;07.10.2021&quot;, &quot;08.10.2021&quot;, &quot;09.10.2021&quot;,
  3. &quot;10.10.2021&quot;, &quot;11.10.2021&quot;, &quot;12.10.2021&quot;, &quot;01.10.2022&quot;), X1 = c(20L,
  4. 26L, 29L, 20L, 20L, 30L, 39L), X2 = c(25L, 25L, 29L, 29L, 30L,
  5. 30L, NA), X3 = c(25L, 25L, 25L, 25L, 30L, NA, NA), X4 = c(26L,
  6. 26L, 26L, 31L, NA, NA, NA), X5 = c(26L, 25L, 39L, NA, NA, NA,
  7. NA), X6 = c(25L, 31L, NA, NA, NA, NA, NA), X7 = c(31L, NA, NA,
  8. NA, NA, NA, NA)), class = &quot;data.frame&quot;, row.names = c(NA, 7L))
  1. <details>
  2. <summary>英文:</summary>
  3. one approach:

(d[,8:3] |> apply(2, cumsum)/
d[,7:2] |> apply(2, cumsum)
) |>
diag() |>
rev() |>
setNames(nm = names(d)[-c(1:2)])

  1. result:

X2 X3 X4 X5 X6 X7

1.158621 0.942029 1.090000 1.153846 1.098039 1.240000

  1. where `d` is your dataframe:

d <-
structure(list(Date = c("07.10.2021", "08.10.2021", "09.10.2021",
"10.10.2021", "11.10.2021", "12.10.2021", "01.10.2022"), X1 = c(20L,
26L, 29L, 20L, 20L, 30L, 39L), X2 = c(25L, 25L, 29L, 29L, 30L,
30L, NA), X3 = c(25L, 25L, 25L, 25L, 30L, NA, NA), X4 = c(26L,
26L, 26L, 31L, NA, NA, NA), X5 = c(26L, 25L, 39L, NA, NA, NA,
NA), X6 = c(25L, 31L, NA, NA, NA, NA, NA), X7 = c(31L, NA, NA,
NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 7L))

  1. </details>

huangapple
  • 本文由 发表于 2023年6月6日 05:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410002.html
匿名

发表评论

匿名网友

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

确定