对于一个RMarkdown报告,泛化变量名称

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

Generalizing variable names for an RMarkdown Report

问题

我想要使在RMarkdown报告创建中更容易(减少编辑)。例如,假设我正在调查mtcars。我的当前工作流程是为每加仑英里数(mpg)创建一个摘要统计数据。

  1. library(tidyverse)
  2. library(rstatix)
  3. df <- mtcars %>% get_summary_stats(mpg, type = "mean_sd")
  4. mtcars <- mtcars %>% mutate(division_construct = case_when(
  5. mpg< 7.5 ~ "Low",
  6. between(mpg,7.5, 11.50) ~ "Below Average",
  7. between(mpg,11.501, 15.75) ~ "Above Average",
  8. mpg> 15.75 ~ "High",
  9. TRUE ~ "Error"))

现在假设我想要另一个列名,我必须手动进行编辑。相反,我想要做类似这样的事情:

  1. var <- "mpg"
  2. df <- mtcars %>% get_summary_stats(var, type = "mean_sd")
  3. mtcars <- mtcars %>% mutate(division_construct = case_when(
  4. var < 7.5 ~ "Low",
  5. between(var,7.5, 11.50) ~ "Below Average",
  6. between(var,11.501, 15.75) ~ "Above Average",
  7. var > 15.75 ~ "High",
  8. TRUE ~ "Error"))

当我尝试运行代码时,我得到以下错误:

  1. Error in `mutate()`:
  2. In argument: `division_construct = case_when(...)`.
  3. Caused by error in `case_when()`:
  4. ! Failed to evaluate the left-hand side of formula 2.
  5. Caused by error in `between()`:
  6. ! Can't combine `x` <character> and `left` <double>.

我认为错误是因为var本质上是一个字符串而不是一个列名。

英文:

I want to make it easier (make fewer edits) in an RMarkdown report creation. For example, let's say I am investigating mtcars. My current workflow is to create a summary statistic for miles per gallon (mpg).

  1. library(tidyverse)
  2. library(rstatix)
  3. df &lt;- mtcars %&gt;% get_summary_stats(mpg, type = &quot;mean_sd&quot;)
  4. mtcars &lt;- mtcars %&gt;% mutate(division_construct = case_when(
  5. mpg&lt; 7.5 ~ &quot;Low&quot;,
  6. between(mpg,7.5, 11.50) ~ &quot;Below Average&quot;,
  7. between(mpg,11.501, 15.75) ~ &quot;Above Average&quot;,
  8. mpg&gt; 15.75 ~ &quot;High&quot;,
  9. TRUE ~ &quot;Error&quot;))

Now let's say I want another column name, I have to manually make edits. Instead,
I want to do something like this:

  1. var &lt;- &quot;mpg&quot;
  2. df &lt;- mtcars %&gt;% get_summary_stats(var, type = &quot;mean_sd&quot;)
  3. mtcars &lt;- mtcars %&gt;% mutate(division_construct = case_when(
  4. var &lt; 7.5 ~ &quot;Low&quot;,
  5. between(var,7.5, 11.50) ~ &quot;Below Average&quot;,
  6. between(var,11.501, 15.75) ~ &quot;Above Average&quot;,
  7. var &gt; 15.75 ~ &quot;High&quot;,
  8. TRUE ~ &quot;Error&quot;))

When I try to run the code, I get the following error:

  1. Error in `mutate()`:
  2. In argument: `division_construct = case_when(...)`.
  3. Caused by error in `case_when()`:
  4. ! Failed to evaluate the left-hand side of formula 2.
  5. Caused by error in `between()`:
  6. ! Can&#39;t combine `x` &lt;character&gt; and `left` &lt;double&gt;.

I think the error is because var is essentially a string and not a column name.

答案1

得分: 1

你可能想要将代码包装在一个函数中:

  1. library(dplyr)
  2. my_function <- function(var){
  3. df1 <-
  4. mtcars %>%
  5. mutate(division_construct = case_when(
  6. {{var}} < 7.5 ~ "低",
  7. between( {{var}}, 7.5, 11.50) ~ "低于平均",
  8. between( {{var}}, 11.501, 15.75) ~ "高于平均",
  9. {{var}} > 15.75 ~ "高",
  10. TRUE ~ "错误"))
  11. return(df1)
  12. }
  13. my_function(var = mpg)[1:10, c("mpg", "division_construct")]
  14. #> mpg division_construct
  15. #> Mazda RX4 21.0 高
  16. #> Mazda RX4 Wag 21.0 高
  17. #> Datsun 710 22.8 高
  18. #> Hornet 4 Drive 21.4 高
  19. #> Hornet Sportabout 18.7 高
  20. #> Valiant 18.1 高
  21. #> Duster 360 14.3 高于平均
  22. #> Merc 240D 24.4 高
  23. #> Merc 230 22.8 高
  24. #> Merc 280 19.2 高

创建于2023年06月19日,使用 reprex v2.0.2

英文:

You may want to wrap the code in a function:

  1. library(dplyr)
  2. my_function &lt;- function(var){
  3. df1 &lt;-
  4. mtcars |&gt;
  5. mutate(division_construct = case_when(
  6. {{var}} &lt; 7.5 ~ &quot;Low&quot;,
  7. between( {{var}}, 7.5, 11.50) ~ &quot;Below Average&quot;,
  8. between( {{var}}, 11.501, 15.75) ~ &quot;Above Average&quot;,
  9. {{var}} &gt; 15.75 ~ &quot;High&quot;,
  10. TRUE ~ &quot;Error&quot;))
  11. return(df1)
  12. }
  13. my_function(var = mpg)[1:10, c(&quot;mpg&quot;, &quot;division_construct&quot;)]
  14. #&gt; mpg division_construct
  15. #&gt; Mazda RX4 21.0 High
  16. #&gt; Mazda RX4 Wag 21.0 High
  17. #&gt; Datsun 710 22.8 High
  18. #&gt; Hornet 4 Drive 21.4 High
  19. #&gt; Hornet Sportabout 18.7 High
  20. #&gt; Valiant 18.1 High
  21. #&gt; Duster 360 14.3 Above Average
  22. #&gt; Merc 240D 24.4 High
  23. #&gt; Merc 230 22.8 High
  24. #&gt; Merc 280 19.2 High

<sup>Created on 2023-06-19 with reprex v2.0.2</sup>

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

发表评论

匿名网友

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

确定