英文:
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 <-
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'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 <- function(.x, .y) {
keep_rows <- 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 <-
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>
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论