Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在使用mutate_时在函数中使用变量名_R_Dplyr_Mutate - Fatal编程技术网

R 在使用mutate_时在函数中使用变量名

R 在使用mutate_时在函数中使用变量名,r,dplyr,mutate,R,Dplyr,Mutate,我想创建一个函数,可以根据用户提供的输入变量和截止值列表以编程方式添加变量 out1 <- df %>% mutate( Sepal.Length_indicator = (Sepal.Length <= 5), Petal.Length_indicator = (Petal.Length <= 1.5) ) identical(out1, out2) #[1] TRUE 具体来

我想创建一个函数,可以根据用户提供的输入变量和截止值列表以编程方式添加变量

out1 <- df %>%
         mutate(
           Sepal.Length_indicator = (Sepal.Length <= 5),
           Petal.Length_indicator = (Petal.Length <= 1.5)
          )    

identical(out1, out2)
#[1] TRUE
具体来说,我想定义一个函数

myfun <- function(df, varlist, cutofflist)

out1 <- df %>%
         mutate(
           Sepal.Length_indicator = (Sepal.Length <= 5),
           Petal.Length_indicator = (Petal.Length <= 1.5)
          )    

identical(out1, out2)
#[1] TRUE
我想要这个电话

myfun(df, c("Sepal.Length", "Petal.Length"), list(Sepal.Length = 5, Petal.Length = 1.5))
out1 <- df %>%
         mutate(
           Sepal.Length_indicator = (Sepal.Length <= 5),
           Petal.Length_indicator = (Petal.Length <= 1.5)
          )    

identical(out1, out2)
#[1] TRUE
产生与…相同的结果

df %>%
   mutate(
      Sepal.Length_indicator = (Sepal.Length <= 5),
      Petal.Length_indicator = (Petal.Length <= 1.5)
   )
out1 <- df %>%
         mutate(
           Sepal.Length_indicator = (Sepal.Length <= 5),
           Petal.Length_indicator = (Petal.Length <= 1.5)
          )    

identical(out1, out2)
#[1] TRUE
df%>%
变异(

萼片长度指示器=(萼片长度这里有一个选项带有
map2
transmute

library(tidyverse)
myfun <- function(data, varVec, cutofflist) {
    map2_dfc(varVec, cutofflist[varVec], ~   

                     data %>% 
                        transmute( !! paste0(.x, "_indicator") := 
                               !! rlang::sym(.x) <= .y)) %>%
                 bind_cols(df, .)

     }


out2 <- myfun(df, c("Sepal.Length", "Petal.Length"), 
        list(Sepal.Length = 5, Petal.Length = 1.5))   
out1 <- df %>%
         mutate(
           Sepal.Length_indicator = (Sepal.Length <= 5),
           Petal.Length_indicator = (Petal.Length <= 1.5)
          )    

identical(out1, out2)
#[1] TRUE