R语言 case_when和/或ifelse中的引号

rn0zuynd  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(142)

我正在尝试整理如何使用quosure(如果这是正确的工具)将变量名传递给mutate命令中的if_else(...)或case_when(...),该命令使用从函数传递来的字符串参数。

#create a simple 3x3 tibble
library(tidyverse)
lev<-c("a","b","c")
a=seq(1,3)
test<-tibble("index"=lev,"raw"=as.numeric(a),"x2"=a*2, x3 = a*3)

现在,假设我想在index==“a”的情况下用零替换“raw”的值,我可以用原始代码来完成:

test %>% 
  mutate(raw=case_when(
  (index=="a")~0,
  TRUE~raw
)
)

我得到输出:

# A tibble: 3 x 4
  index   raw    x2    x3
  <chr> <dbl> <dbl> <dbl>
1 a         0     2     3
2 b         2     4     6
3 c         3     6     9

很好。我可以用两种不同的方法在函数中实现这一点(if_else或case_when)。首先:

sending_test_cw<-function(data_sent)
{
  data_sent %>% 
    mutate(raw=case_when(
      (index=="a")~0,
      TRUE~raw)
    )
}

产出:

sending_test_cw(test)

R > sending_test_cw(test)
# A tibble: 3 x 4
  index   raw    x2    x3
  <chr> <dbl> <dbl> <dbl>
1 a         0     2     3
2 b         2     4     6
3 c         3     6     9

或者,对于case_when:

sending_test_ie<-function(data_sent)
{
  data_sent %>% 
    mutate(
    raw=ifelse(index=="a",0,raw))
}

R > sending_test_ie(test)
# A tibble: 3 x 4
  index   raw    x2    x3
  <chr> <dbl> <dbl> <dbl>
1 a         0     2     3
2 b         2     4     6
3 c         3     6     9

我又得到了预期的输出。
现在,我想创建一个函数,它在发送保存索引的列的name时工作,类似于:

sending_test_qu<-function(data_sent,index_id="index")
{
  index_quo<-enquo(index_id)
  data_sent %>% 
    #group_by(index)%>%
    mutate(
      raw=ifelse(!!index_quo=="a",0,raw),
      raw_2=case_when(
        (!!index_quo=="a")~0,
        TRUE~raw)
    )
}

sending_test_qu(test)

但是,我不能得到工作的工作。

sending_test_qu<-function(data_sent,index_id="index")
{
  index_quo<-enquo(index_id)
  data_sent %>% 
    #group_by(index)%>%
    mutate(
      raw=ifelse(!!index_quo=="a",0,raw),
      raw_2=case_when(
        (!!index_quo=="a")~0,
        TRUE~raw)
    )
}

sending_test_qu(test)

这产生如下输出:

R > sending_test_qu(test)
# A tibble: 3 x 5
  index   raw    x2    x3 raw_2
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a         1     2     3     1
2 b         1     4     6     1
3 c         1     6     9     1

欢迎任何建议或引用。

goucqfw6

goucqfw61#

转换为sym bol with ensym,因为输入是字符串(或者也可以是无引号的),如果输入是无引号的,可以使用enquo with !!,或者更直接地使用{{}}

sending_test_qu<-function(data_sent,index_id="index")
{
  index_sym<- rlang::ensym(index_id)
  data_sent %>% 
    #group_by(across(all_of(index_id)))%>%
    mutate(
      raw=ifelse(!!index_sym=="a",0,raw),
      raw_2=case_when(
        (!!index_sym=="a")~0,
        TRUE~raw)
    )
}

测试

# default argument value for index_id
> sending_test_qu(test)
 A tibble: 3 × 5
  index   raw    x2    x3 raw_2
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a         0     2     3     0
2 b         2     4     6     2
3 c         3     6     9     3
# pass as unquoted
> sending_test_qu(test, index)
# A tibble: 3 × 5
  index   raw    x2    x3 raw_2
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a         0     2     3     0
2 b         2     4     6     2
3 c         3     6     9     3
# pass as string
> sending_test_qu(test, "index")
# A tibble: 3 × 5
  index   raw    x2    x3 raw_2
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a         0     2     3     0
2 b         2     4     6     2
3 c         3     6     9     3

相关问题