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

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

Generalizing variable names for an RMarkdown Report

问题

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

library(tidyverse)
library(rstatix)

df <- mtcars %>% get_summary_stats(mpg, type = "mean_sd")

mtcars <- mtcars %>% mutate(division_construct = case_when(
  mpg< 7.5 ~ "Low",
  between(mpg,7.5, 11.50) ~ "Below Average",
  between(mpg,11.501, 15.75) ~ "Above Average",
  mpg> 15.75 ~ "High",
  TRUE ~ "Error"))

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

var <- "mpg"

df <- mtcars %>% get_summary_stats(var, type = "mean_sd")

mtcars <- mtcars %>% mutate(division_construct = case_when(
  var < 7.5 ~ "Low",
  between(var,7.5, 11.50) ~ "Below Average",
  between(var,11.501, 15.75) ~ "Above Average",
  var > 15.75 ~ "High",
  TRUE ~ "Error"))

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

Error in `mutate()`:
ℹ In argument: `division_construct = case_when(...)`.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 2.
Caused by error in `between()`:
! 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).

library(tidyverse)
library(rstatix)


df &lt;- mtcars %&gt;% get_summary_stats(mpg, type = &quot;mean_sd&quot;)

mtcars &lt;- mtcars %&gt;% mutate(division_construct = case_when(
  mpg&lt; 7.5 ~ &quot;Low&quot;,
  between(mpg,7.5, 11.50) ~ &quot;Below Average&quot;,
  between(mpg,11.501, 15.75) ~ &quot;Above Average&quot;,
  mpg&gt; 15.75 ~ &quot;High&quot;,
  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:

var &lt;- &quot;mpg&quot;

df &lt;- mtcars %&gt;% get_summary_stats(var, type = &quot;mean_sd&quot;)

mtcars &lt;- mtcars %&gt;% mutate(division_construct = case_when(
  var &lt; 7.5 ~ &quot;Low&quot;,
  between(var,7.5, 11.50) ~ &quot;Below Average&quot;,
  between(var,11.501, 15.75) ~ &quot;Above Average&quot;,
  var &gt; 15.75 ~ &quot;High&quot;,
  TRUE ~ &quot;Error&quot;))

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

Error in `mutate()`:
ℹ In argument: `division_construct = case_when(...)`.
Caused by error in `case_when()`:
! Failed to evaluate the left-hand side of formula 2.
Caused by error in `between()`:
! 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

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

library(dplyr)

my_function <- function(var){
  df1 <-
    mtcars %>%
    mutate(division_construct = case_when(
      {{var}} < 7.5 ~ "低",
      between( {{var}}, 7.5, 11.50) ~ "低于平均",
      between( {{var}}, 11.501, 15.75) ~ "高于平均",
      {{var}} > 15.75 ~ "高",
      TRUE ~ "错误"))
  return(df1)
}

my_function(var = mpg)[1:10, c("mpg", "division_construct")]
#>                    mpg division_construct
#> Mazda RX4         21.0                 高
#> Mazda RX4 Wag     21.0                 高
#> Datsun 710        22.8                 高
#> Hornet 4 Drive    21.4                 高
#> Hornet Sportabout 18.7                 高
#> Valiant           18.1                 高
#> Duster 360        14.3               高于平均
#> Merc 240D         24.4                 高
#> Merc 230          22.8                 高
#> Merc 280          19.2                 高

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

英文:

You may want to wrap the code in a function:

library(dplyr)

my_function &lt;- function(var){
  df1 &lt;-
    mtcars |&gt;  
    mutate(division_construct = case_when(
      {{var}} &lt; 7.5 ~ &quot;Low&quot;,
      between( {{var}}, 7.5, 11.50) ~ &quot;Below Average&quot;,
      between( {{var}}, 11.501, 15.75) ~ &quot;Above Average&quot;,
      {{var}} &gt; 15.75 ~ &quot;High&quot;,
      TRUE ~ &quot;Error&quot;))
  return(df1)
}


my_function(var = mpg)[1:10, c(&quot;mpg&quot;, &quot;division_construct&quot;)]
#&gt;                    mpg division_construct
#&gt; Mazda RX4         21.0               High
#&gt; Mazda RX4 Wag     21.0               High
#&gt; Datsun 710        22.8               High
#&gt; Hornet 4 Drive    21.4               High
#&gt; Hornet Sportabout 18.7               High
#&gt; Valiant           18.1               High
#&gt; Duster 360        14.3      Above Average
#&gt; Merc 240D         24.4               High
#&gt; Merc 230          22.8               High
#&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:

确定