英文:
How to Calculate the Percentage Contribution Within Rows in a Data Frame in R
问题
I apologize for the confusion, but it seems that your request is to provide a translation of the content you provided, without addressing any specific questions or issues in the code. Here is the translated content:
我想知道,我们是否可以计算数据帧中行内值的百分比贡献。
我正在使用的数据帧如下所示:
structure(list(`Row Labels` = c("X1", "X2", "X3", "X4"), `2019-01-01` = c(37,
36, 45, 53), `2019-02-01` = c(3, 19, 14, 46), `2019-03-01` = c(28,
2, 28, 28), `2019-04-01` = c(48, 70, 18, 16), `2019-05-01` = c(83,
71, 58, 26), `2019-06-01` = c(85, 28, 83, 46), `2019-07-01` = c(60,
20, 12, 77), `2019-08-01` = c(44, 66, 30, 99), `2019-09-01` = c(21,
14, 31, 21), `2019-10-01` = c(26, 72, 72, 16), `2019-11-01` = c(15,
96, 23, 100), `2019-12-01` = c(65, 0, 98, 66)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
我编写的代码如下:
Book1 <- read_excel("X:/X/X/X - X/X/Book1.xlsx")
First_Date <- "2019-01-01"
Last_Date <- "2019-12-01"
Book1 <- Book1 %>%
mutate(Sum = rowSums(pick(any_of(First_Date):any_of(Last_Date)))) %>%
mutate(across(pick(any_of(First_Date):any_of(Last_Date), ~./rowSums(pick(any_of(First_Date):any_of(Last_Date)))),.names = "{.col}_%"))
运行此代码时,我收到以下错误:
Error in `mutate()`: ℹ In argument: `across(...)`. Caused by error in `pick()`: ! Formula shorthand must be wrapped in `where()`.
# Bad data %>% select(~./rowSums(pick(any_of(First_Date):any_of(Last_Date))))
# Good data %>% select(where(~./rowSums(pick(any_of(First_Date):any_of(Last_Date))))))
以上的代码应该获得以下图像的输出:
但如果输出以这种格式呈现,我不介意:
有人可以告诉我在计算贡献时出了什么问题吗?这将会很有帮助。是否有更简单的方法来做这个?
英文:
I wanted to know, if we can calculate the percentage contribution of the values within a row in a data frame.
The data frame I am working with is:
structure(list(`Row Labels` = c("X1", "X2", "X3", "X4"), `2019-01-01` = c(37,
36, 45, 53), `2019-02-01` = c(3, 19, 14, 46), `2019-03-01` = c(28,
2, 28, 28), `2019-04-01` = c(48, 70, 18, 16), `2019-05-01` = c(83,
71, 58, 26), `2019-06-01` = c(85, 28, 83, 46), `2019-07-01` = c(60,
20, 12, 77), `2019-08-01` = c(44, 66, 30, 99), `2019-09-01` = c(21,
14, 31, 21), `2019-10-01` = c(26, 72, 72, 16), `2019-11-01` = c(15,
96, 23, 100), `2019-12-01` = c(65, 0, 98, 66)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
The code which I wrote for this is given below:
Book1 <- read_excel("X:/X/X/X - X/X/Book1.xlsx")
First_Date <- "2019-01-01"
Last_Date <- "2019-12-01"
Book1 <- Book1 %>%
mutate(Sum = rowSums(pick(any_of(First_Date):any_of(Last_Date)))) %>%
mutate(across(pick(any_of(First_Date):any_of(Last_Date), ~./rowSums(pick(any_of(First_Date):any_of(Last_Date)))),.names = "{.col}_%"))
When I am running this code, the error I get is:
Error in `mutate()`: ℹ In argument: `across(...)`. Caused by error in `pick()`: ! Formula shorthand must be wrapped in `where()`.
# Bad data %>% select(~./rowSums(pick(any_of(First_Date):any_of(Last_Date))))
# Good data %>% select(where(~./rowSums(pick(any_of(First_Date):any_of(Last_Date)))))
The code above should get the output of the below image
but I dont mind if the output is in this format as well
Can someone let me know what is it that i am getting wrong to find the contribution? It would be helpful. Is there a simpler way of doing it?
答案1
得分: 1
对于使用dplyr
的第二个输出,您可以:
library(dplyr)
df %>%
rowwise() %>%
mutate(across(where(is.numeric), ~ .x / sum(across(where(is.numeric)))))
# A tibble: 4 × 13
# Rowwise:
`Row Labels` `2019-01-01` `2019-02-01` `2019-03-01` `2019-04-01` `2019-05-01` `2019-06-01` `2019-07-01` `2019-08-01` `2019-09-01` `2019-10-01` `2019-11-01`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 X1 0.0718 0.00583 0.0544 0.0932 0.161 0.165 0.117 0.0854 0.0408 0.0505 0.0291
2 X2 0.0729 0.0385 0.00405 0.142 0.144 0.0567 0.0405 0.134 0.0283 0.146 0.194
3 X3 0.0879 0.0273 0.0547 0.0352 0.113 0.162 0.0234 0.0586 0.0605 0.141 0.0449
4 X4 0.0892 0.0774 0.0471 0.0269 0.0438 0.0774 0.130 0.167 0.0354 0.0269 0.168
# ℹ 1 more variable: `2019-12-01` <dbl>
乘以100:
df %>%
rowwise() %>%
mutate(across(
where(is.numeric), ~ .x / sum(across(where(is.numeric))) * 100
))
英文:
For the second output with dplyr
you can:
library(dplyr)
df %>%
rowwise() %>%
mutate(across(where(is.numeric), ~ .x / sum(across(where(is.numeric)))))
# A tibble: 4 × 13
# Rowwise:
`Row Labels` `2019-01-01` `2019-02-01` `2019-03-01` `2019-04-01` `2019-05-01` `2019-06-01` `2019-07-01` `2019-08-01` `2019-09-01` `2019-10-01` `2019-11-01`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 X1 0.0718 0.00583 0.0544 0.0932 0.161 0.165 0.117 0.0854 0.0408 0.0505 0.0291
2 X2 0.0729 0.0385 0.00405 0.142 0.144 0.0567 0.0405 0.134 0.0283 0.146 0.194
3 X3 0.0879 0.0273 0.0547 0.0352 0.113 0.162 0.0234 0.0586 0.0605 0.141 0.0449
4 X4 0.0892 0.0774 0.0471 0.0269 0.0438 0.0774 0.130 0.167 0.0354 0.0269 0.168
# ℹ 1 more variable: `2019-12-01` <dbl>
Multiply with 100:
df %>%
rowwise() %>%
mutate(across(
where(is.numeric), ~ .x / sum(across(where(is.numeric))) * 100
))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论