在 purrr 中为用户定义的函数提供非引号参数

Giving user defined functions non-quoted arguments in purrr

我正在学习使用 . My function looks at if any forbidden_values occur in any of the mentioned 个变量 selectlist 的准引用和引用,并将具有任何正结果的行标记为 1,否则 0.

此函数仅适用于在引号 " " 中选择列表的变量名称。

selectlist <- function(df, selectlist, forbidden_values){
  enquos(selectlist)
  enquos(forbidden_values)


  df %>% mutate(is_injured = purrr::pmap_int(select(.,!!selectlist), ~any(c(...) %in% !!forbidden_values)))

}

有效

selectlist(expected, selectlist=c("PrimaryInjury","SecondaryInjury"),forbidden_values=c("Rust","Insect","Snow break"))
  plantid year PrimaryInjury SecondaryInjury OtherInjury is_injured
1        1    1          <NA>            <NA>        <NA>          0
2        1    2        Insect            <NA>        <NA>          1
3        1    3          <NA>            <NA>        <NA>          0
4        2    1          <NA>            <NA>        <NA>          0
5        2    2          <NA>            <NA>        <NA>          0
6        2    3          Rust            <NA>        <NA>          1
7        3    1          <NA>            <NA>        <NA>          0
8        3    2          <NA>            <NA>        <NA>          0
9        3    3          <NA>            <NA>        <NA>          0
10       4    1          Rust            <NA>        <NA>          1
11       4    2          <NA>            <NA>        <NA>          0
12       4    3        Insect            <NA>        <NA>          1
13       5    1    Snow break            <NA>        <NA>          1
14       5    2          Rust            <NA>        <NA>          1
15       5    3          <NA>            <NA>        <NA>          0

不起作用

selectlist(expected, selectlist=c(PrimaryInjury,SecondaryInjury),forbidden_values=c("Rust","Insect","Snow break"))

Error in splice(dot_call(capture_dots, frame_env = frame_env, named = named, : object 'PrimaryInjury' not found

感谢您的帮助...

示例数据框:预期

plantid <- rep(c(1,2,3,4,5), times=c(3,3,3,3,3))
year <- rep(1:3, length.out=length(plantid))
set.seed(42)
PrimaryInjury <- sample(c(NA,NA,NA,"Rust","Insect","Snow break"), 15, replace=TRUE)
SecondaryInjury <- rep(NA, length.out=length(plantid))
OtherInjury <- rep(NA, length.out=length(plantid))

expected <- data.frame(plantid,year,PrimaryInjury,SecondaryInjury, OtherInjury)

#All in selectlist must be characters.
expected$PrimaryInjury <- as.character(expected$PrimaryInjury)
expected$SecondaryInjury <- as.character(expected$SecondaryInjury)
expected$OtherInjury <- as.character(expected$OtherInjury)

dput of expected

structure(list(plantid = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 
5, 5, 5), year = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L), PrimaryInjury = c(NA, "Insect", NA, NA, NA, 
"Rust", NA, NA, NA, "Rust", NA, "Insect", "Snow break", "Rust", 
NA), SecondaryInjury = c(NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_), OtherInjury = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
-15L), class = "data.frame")

如果单独传递不带引号的变量可以,我们可以用三个点

selectlist <- function(df, forbidden_values, ...){

    df %>% mutate(is_injured = purrr::pmap_int(select(.,...), 
                               ~any(c(...) %in% forbidden_values)))
}

selectlist(expected, forbidden_values= c("Rust","Insect","Snow break"), 
                      PrimaryInjury, SecondaryInjury)


#   plantid year PrimaryInjury SecondaryInjury OtherInjury is_injured
#1        1    1          <NA>            <NA>        <NA>          0
#2        1    2        Insect            <NA>        <NA>          1
#3        1    3          <NA>            <NA>        <NA>          0
#4        2    1          <NA>            <NA>        <NA>          0
#5        2    2          <NA>            <NA>        <NA>          0
#6        2    3          Rust            <NA>        <NA>          1
#7        3    1          <NA>            <NA>        <NA>          0
#8        3    2          <NA>            <NA>        <NA>          0
#9        3    3          <NA>            <NA>        <NA>          0
#10       4    1          Rust            <NA>        <NA>          1
#11       4    2          <NA>            <NA>        <NA>          0
#12       4    3        Insect            <NA>        <NA>          1
#13       5    1    Snow break            <NA>        <NA>          1
#14       5    2          Rust            <NA>        <NA>          1
#15       5    3          <NA>            <NA>        <NA>          0