英文:
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 <- 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"))
Now let's say I want another column name, I have to manually make edits. Instead,
I want to do something like this:
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"))
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't combine `x` <character> and `left` <double>.
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 <- function(var){
df1 <-
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"))
return(df1)
}
my_function(var = mpg)[1:10, c("mpg", "division_construct")]
#> mpg division_construct
#> Mazda RX4 21.0 High
#> Mazda RX4 Wag 21.0 High
#> Datsun 710 22.8 High
#> Hornet 4 Drive 21.4 High
#> Hornet Sportabout 18.7 High
#> Valiant 18.1 High
#> Duster 360 14.3 Above Average
#> Merc 240D 24.4 High
#> Merc 230 22.8 High
#> Merc 280 19.2 High
<sup>Created on 2023-06-19 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论