首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何取消引用str_detect()中的quosure来过滤r中函数的数据?

如何取消引用str_detect()中的quosure来过滤r中函数的数据?
EN

Stack Overflow用户
提问于 2020-03-05 19:22:14
回答 1查看 136关注 0票数 0

我想要创建一个函数,这将节省我大量的时间打字或复制和粘贴。我正在创建这个函数,以便以后可以将其用于其他分析。

函数

代码语言:javascript
复制
extractItemsForScales <- function(df=NULL, col_name_to_detect_scales=NULL, col_name_to_extract=NULL, scales=NULL, name_suffix=""){



  if(is.null(df))
    stop("Dataframe must not be null")

  if(is.null(scales))
    stop("Scales must not be null")

  if(is.null(col_name_to_detect_scales))
    stop("You must provide the column name to detect scales")

  if(is.null(col_name_to_extract))
    stop("You must provide the column name to extract the items from")

  col_name_to_detect_scales <- enquo(col_name_to_detect_scales)
  col_name_to_extract <- enquo(col_name_to_extract)

  cat("Extracting items")

  vars <- scales %>%
    map(function(x){
      df %>% filter(str_detect(!!col_name_to_detect_scales, x)) %>% pull(!!col_name_to_extract)
    }) %>%
    setNames(paste0(tolower(scales),name_suffix))

  return(vars)
}

数据

代码语言:javascript
复制
df <- structure(list(Scale = c("S01", "S05", "S05", "S01", "S01", "S11", 
"S15", "S15", "S16", "S16", "S05", "S04", "S10", "S13", "S13", 
"S05", "S10", "S09", "S07", "S07", "S06", "S06", "S06", "S07", 
"S11", "S09", "S11", "S04", "S09", "S09", "S07", "S06", "S05", 
"S05", "S06", "S05", "S01", "S01", "S01", "S12", "S01", "S02", 
"S08", "S12", "S08", "S08", "S05", "S04"), SectionNo = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    Item = c("S1_23", "S1_29", "S1_36", "S1_41", "S1_46", "S1_6", 
    "S1_81", "S1_89", "S1_40", "S1_51", "S1_12", "S1_15", "S1_34", 
    "S1_44", "S1_50", "S1_78", "S1_73", "S1_77", "S1_31", "S1_59", 
    "S1_67", "S1_83", "S1_86", "S1_90", "S1_10", "S1_11", "S1_19", 
    "S1_26", "S1_30", "S1_45", "S1_49", "S1_61", "S1_62", "S1_91", 
    "S1_20", "S1_8", "S1_9", "S1_14", "S1_22", "S1_25", "S1_33", 
    "S1_53", "S1_54", "S1_92", "S1_63", "S1_64", "S1_71", "S1_47"
    )), class = "data.frame", row.names = c(NA, -48L))

期望输出

代码语言:javascript
复制
    list(S01_48 = c("S1_23", "S1_41", "S1_46", 
"S1_9", "S1_14", "S1_22", "S1_33"), S05_48 = c("S1_29", 
"S1_36", "S1_12", "S1_78", "S1_62", "S1_91", 
"S1_8", "S1_71"), S11_48 = c("S1_6", "S1_10", 
"S1_19"), S15_48 = c("S1_81", "S1_89"), S16_48 = c("S1_40", 
"S1_51"), S04_48 = c("S1_15", "S1_26", "S1_47"
), S10_48 = c("S1_34", "S1_73"), S13_48 = c("S1_44", 
"S1_50"), S09_48 = c("S1_77", "S1_11", "S1_30", 
"S1_45"), S07_48 = c("S1_31", "S1_59", 
"S1_90", "S1_49"), S06_48 = c("S1_67", "S1_83", 
"S1_86", "S1_61", "S1_20"), S12_48 = c("S1_25", 
"S1_92"), S02_48 = "S1_53", S08_48 = c("S1_54", 
"S1_63", "S1_64"))

函数调用

我会按如下方式调用该函数:

代码语言:javascript
复制
scales <- structure(list(Scale = c("S01", "S05", "S11", "S15", "S16", "S04", 
    "S10", "S13", "S09", "S07", "S06", "S12", "S02", "S08")), class = "data.frame", row.names = c(NA, 
    -14L))

df %>% extractItemsForScales(col_name_to_detect_scales = Scale, col_name_to_extract = Item, scales = scales, name_suffix = "_48")

但是我不断地发现这个错误:

代码语言:javascript
复制
Error in extractItemsForScales(., col_name_to_detect_scales = Scale, col_name_to_extract = Item,  : 
  object 'Item' not found

我为什么要犯这个错误?我怎么才能解决这个问题?

更新

调试时,似乎在is.null()检查col_name_to_extract期间抛出了错误。但是,我不知道为什么在此检查中出现错误,而不是上面对col_name_to_detect_scales的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-05 20:45:18

我认为你的不引用是可以的。如果将if(is.null更改为if(missing,此函数将按预期工作。您还为scale传递了错误的值,因为您传递的对象似乎是一个数据框架,所以不是按Map中的每个字符串映射,而是按列进行映射。

所以如果你有

代码语言:javascript
复制
extractItemsForScales <- function(df, 
                                  col_name_to_detect_scales, 
                                  col_name_to_extract, 
                                  scales, 
                                  name_suffix=""){

  if(missing(df)) 
    stop("Dataframe must not be null")

  if(missing(scales)) 
    stop("Scales must not be null")

  if(missing(col_name_to_detect_scales))
    stop("You must provide the column name to detect scales")

  if(missing(col_name_to_extract))
    stop("You must provide the column name to extract the items from")

  col_name_to_detect_scales <- enquo(col_name_to_detect_scales)
  col_name_to_extract <- enquo(col_name_to_extract)

  cat("Extracting items\n\n")

  vars <- scales %>%
    map(function(x){
      df %>% 
        filter(str_detect(!!col_name_to_detect_scales, x)) %>% 
        pull(!!col_name_to_extract)
    }) %>%
    setNames(paste0(tolower(scales),name_suffix))

  return(vars)
}

而你却有

代码语言:javascript
复制
df %>% extractItemsForScales(col_name_to_detect_scales = Scale, 
                             col_name_to_extract = Item, 
                             scales = scales$Scale, 
                             name_suffix = "_48")

你会得到

代码语言:javascript
复制
#> Extracting items
#> 
#> $s01_48
#> [1] "S1_23" "S1_41" "S1_46" "S1_9"  "S1_14" "S1_22" "S1_33"
#> 
#> $s05_48
#> [1] "S1_29" "S1_36" "S1_12" "S1_78" "S1_62" "S1_91" "S1_8"  "S1_71"
#> 
#> $s11_48
#> [1] "S1_6"  "S1_10" "S1_19"
#> 
#> $s15_48
#> [1] "S1_81" "S1_89"
#> 
#> $s16_48
#> [1] "S1_40" "S1_51"
#> 
#> $s04_48
#> [1] "S1_15" "S1_26" "S1_47"
#> 
#> $s10_48
#> [1] "S1_34" "S1_73"
#> 
#> $s13_48
#> [1] "S1_44" "S1_50"
#> 
#> $s09_48
#> [1] "S1_77" "S1_11" "S1_30" "S1_45"
#> 
#> $s07_48
#> [1] "S1_31" "S1_59" "S1_90" "S1_49"
#> 
#> $s06_48
#> [1] "S1_67" "S1_83" "S1_86" "S1_61" "S1_20"
#> 
#> $s12_48
#> [1] "S1_25" "S1_92"
#> 
#> $s02_48
#> [1] "S1_53"
#> 
#> $s08_48
#> [1] "S1_54" "S1_63" "S1_64"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60552612

复制
相关文章

相似问题

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