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

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

Sum up columns and divide them by adjacent cell

问题

以下是您要翻译的内容:

## example data d:
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))

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

## 示例数据 d:
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))

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

英文:

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
## example data d:
d &lt;- 
structure(list(Date = c(&quot;07.10.2021&quot;, &quot;08.10.2021&quot;, &quot;09.10.2021&quot;, 
&quot;10.10.2021&quot;, &quot;11.10.2021&quot;, &quot;12.10.2021&quot;, &quot;01.10.2022&quot;), 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 = &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:

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

but that didn't work

答案1

得分: 1

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

一个方法:

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


结果:
```R
##       X2       X3       X4       X5       X6       X7 
## 1.158621 0.942029 1.090000 1.153846 1.098039 1.240000 

其中 d 是您的数据框架:

d &lt;- 
structure(list(Date = c(&quot;07.10.2021&quot;, &quot;08.10.2021&quot;, &quot;09.10.2021&quot;, 
&quot;10.10.2021&quot;, &quot;11.10.2021&quot;, &quot;12.10.2021&quot;, &quot;01.10.2022&quot;), 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 = &quot;data.frame&quot;, row.names = c(NA, 7L))

<details>
<summary>英文:</summary>

one approach:

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

result:

X2 X3 X4 X5 X6 X7

1.158621 0.942029 1.090000 1.153846 1.098039 1.240000

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))


</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:

确定