首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用tidyselect在一个具有变异和

使用tidyselect在一个具有变异和
EN

Stack Overflow用户
提问于 2022-08-22 16:19:41
回答 2查看 93关注 0票数 0

我有这样的数据:

代码语言:javascript
复制
(test <- tribble(
  ~case, ~A.1_recipient, ~A.2_recipient, ~A.1_donor, ~A.2_donor,
  1, "HLA-A2", "HLA-A3", "HLA-A2", "HLA-A68",
  2, "A2", "HLA-A3", "A3", "A69",
  3, "A11", "A24", "HLA-A2", NA,
  4, "A66", NA, "A2", "A24"
  ))

我经常使用代码从值中删除前缀:

代码语言:javascript
复制
(test_code <- test
  %>% mutate(A.1_recipient = str_replace(A.1_recipient, "HLA-", ""))
)

我还使用tidyselect将函数应用于多个列:

代码语言:javascript
复制
(test_code <- test
  %>% mutate(across(A.1_recipient:A.2_donor, ~str_replace(., "HLA-", "")))
)

(test_code <- test
  %>% mutate(across(ends_with("recipient"), ~str_replace(., "HLA-", "")))
)

我想把它开发成一个函数,它可以处理单个列的输入或tidyselect。使用此链接(https://cran.r-project.org/web/packages/tidyselect/vignettes/tidyselect.html),我尝试了以下功能:

代码语言:javascript
复制
HLA_prefix_remove <- function(.data, ...) {
    # Remove any HLA and locus prefixes from typing results.
  expr <- rlang::expr(c(...))
  pos <- eval_select(expr, data = .data)
  .data <- .data %>% 
  mutate(.data[pos], ~str_replace(pos, "HLA-", "")) 
}

我试图用以下代码运行该代码:

代码语言:javascript
复制
(test_function <- test
  %>% HLA_prefix_remove(A.1_recipient)
  )

但是,我得到以下错误:

代码语言:javascript
复制
Error in `mutate()`:
! Problem while computing `..1 = .data[pos]`.
Caused by error in `.data[pos]`:
! `[` is not supported by the `.data` pronoun, use `[[` or $
  instead.
Backtrace:
  1. test %>% HLA_prefix_remove(A.1_recipient)
 10. rlang:::`[.rlang_data_pronoun`(.data, pos)
 Error in mutate(., .data[pos], ~str_replace(pos, "HLA-", "")) : 
Caused by error in `.data[pos]`:
! `[` is not supported by the `.data` pronoun, use `[[` or $
 instead.

不知道如何编写函数以正确传递参数?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-22 17:13:40

使用c(.)就像这样:

代码语言:javascript
复制
library(dplyr)
library(stringr)

HLA_prefix_remove2 <- function(.data, ...) {
  .data %>% mutate(across(c(...), ~str_replace(., "HLA-", "")))
}

HLA_prefix_remove2(test, A.1_recipient:A.2_donor)
HLA_prefix_remove2(test, ends_with("recipient"))
HLA_prefix_remove2(test, c(A.1_recipient, A.2_recipient))
HLA_prefix_remove2(test, A.1_recipient, A.2_recipient)

如果有一个单独的第二个参数是可以的,那么这也可以:

代码语言:javascript
复制
HLA_prefix_remove3 <- function(.data, sel) {
  .data %>% mutate(across({{sel}}, ~str_replace(., "HLA-", "")))
}

HLA_prefix_remove3(test, A.1_recipient:A.2_donor)
HLA_prefix_remove3(test, ends_with("recipient"))
HLA_prefix_remove3(test, c(A.1_recipient, A.2_recipient))
票数 2
EN

Stack Overflow用户

发布于 2022-08-22 16:23:49

我们可以将函数更改为

代码语言:javascript
复制
library(rlang)
library(dplyr)
library(purrr)
library(stringr)
HLA_prefix_remove <- function(.data, ...) {
      colnms <- map_chr(rlang::ensyms(...), as_string)
     .data %>% 
         mutate(across(all_of(colnms), ~str_replace(.x, "HLA-", "")))
}

-testing

代码语言:javascript
复制
test %>%
    HLA_prefix_remove(A.1_recipient)
# A tibble: 4 × 5
   case A.1_recipient A.2_recipient A.1_donor A.2_donor
  <dbl> <chr>         <chr>         <chr>     <chr>    
1     1 A2            HLA-A3        HLA-A2    HLA-A68  
2     2 A2            HLA-A3        A3        A69      
3     3 A11           A24           HLA-A2    <NA>     
4     4 A66           <NA>          A2        A24   
test %>%
   HLA_prefix_remove(A.1_recipient, A.2_recipient)
# A tibble: 4 × 5
   case A.1_recipient A.2_recipient A.1_donor A.2_donor
  <dbl> <chr>         <chr>         <chr>     <chr>    
1     1 A2            A3            HLA-A2    HLA-A68  
2     2 A2            A3            A3        A69      
3     3 A11           A24           HLA-A2    <NA>     
4     4 A66           <NA>          A2        A24         
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73448162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档