根据特定列计算百分比。

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

Calculate percentage depending on a certain column

问题

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

  1. 我想知道每一行如何根据列计算百分比?
  2. 这是一个虚拟数据集:
  3. c <- c(10, 20, 30, 40, 50)
  4. b <- c(40, 2, 40, 10, 50)
  5. a <- c(100, 50, 70, 60, 100)
  6. id <- c("a", "b", "c", "d", "e")
  7. data <- data.frame(id, a, b, c)
  8. head(data)
  9. # id a b c
  10. # 1 a 100 40 10
  11. # 2 b 50 2 20
  12. # 3 c 70 40 30
  13. # 4 d 60 10 40
  14. # 5 e 100 50 50
  15. 对于每一行,我们如何设置列 "a" 100% 并根据它来计算列 "b" "c" 的比例?
  16. 以下是预期的输出:
  17. # id a b c proportion_b proportion_c
  18. # 1 a 100 40 10 40 10
  19. # 2 b 50 2 20 4 40
  20. # 3 c 70 40 30 57.14286 42.85714
  21. # 4 d 60 10 40 16.66667 66.66667
  22. # 5 e 100 50 50 50 50
  23. 如果可能的话,更喜欢使用 `tidyverse` 方法。谢谢。
英文:

I would like to know for each row how to calculate percentage depends on the column?

Here is dummy datasets:

  1. c &lt;- c(10, 20, 30, 40, 50)
  2. b &lt;- c(40, 2, 40, 10, 50)
  3. a &lt;- c(100, 50, 70, 60, 100)
  4. id &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;)
  5. data &lt;- data.frame(id, a, b, c)
  6. head(data)
  7. # id a b c
  8. # 1 a 100 40 10
  9. # 2 b 50 2 20
  10. # 3 c 70 40 30
  11. # 4 d 60 10 40
  12. # 5 e 100 50 50

For each row how do we set column "a" is a 100% and depends on that calculate proportion for the column b and c?

Here is the expected output:

  1. # id a b c proportion_b proportion_c
  2. # 1 a 100 40 10 40 10
  3. # 2 b 50 2 20 4 40
  4. # 3 c 70 40 30 57.14286 42.85714
  5. # 4 d 60 10 40 16.66667 66.66667
  6. # 5 e 100 50 50 50 50

If its possible tidyverse approach more preferred. Thank you.

答案1

得分: 11

Using base R

  1. nm1 <- c("b", "c")
  2. data[paste0("proportion_", nm1)] <- data[nm1] / data$a * 100

-output

  1. > data
  2. id a b c proportion_b proportion_c
  3. 1 a 100 40 10 40.00000 10.00000
  4. 2 b 50 2 20 4.00000 40.00000
  5. 3 c 70 40 30 57.14286 42.85714
  6. 4 d 60 10 40 16.66667 66.66667
  7. 5 e 100 50 50 50.00000 50.00000
英文:

Using base R

  1. nm1 &lt;- c(&quot;b&quot;, &quot;c&quot;)
  2. data[paste0(&quot;proportion_&quot;, nm1)] &lt;- data[nm1]/data$a * 100

-output

  1. &gt; data
  2. id a b c proportion_b proportion_c
  3. 1 a 100 40 10 40.00000 10.00000
  4. 2 b 50 2 20 4.00000 40.00000
  5. 3 c 70 40 30 57.14286 42.85714
  6. 4 d 60 10 40 16.66667 66.66667
  7. 5 e 100 50 50 50.00000 50.00000
  8. </details>
  9. # 答案2
  10. **得分**: 7
  11. ```r
  12. library(dplyr)
  13. data %>%
  14. mutate(across(b:c, ~ .x / a * 100, .names = "proportion_{.col}"))
  15. # id a b c proportion_b proportion_c
  16. # 1 a 100 40 10 40.00000 10.00000
  17. # 2 b 50 2 20 4.00000 40.00000
  18. # 3 c 70 40 30 57.14286 42.85714
  19. # 4 d 60 10 40 16.66667 66.66667
  20. # 5 e 100 50 50 50.00000 50.00000
英文:

You can divide a into b and c at the same time with across():

  1. library(dplyr)
  2. data %&gt;%
  3. mutate(across(b:c, ~ .x / a * 100, .names = &quot;proportion_{.col}&quot;))
  4. # id a b c proportion_b proportion_c
  5. # 1 a 100 40 10 40.00000 10.00000
  6. # 2 b 50 2 20 4.00000 40.00000
  7. # 3 c 70 40 30 57.14286 42.85714
  8. # 4 d 60 10 40 16.66667 66.66667
  9. # 5 e 100 50 50 50.00000 50.00000

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

发表评论

匿名网友

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

确定