lundi 27 novembre 2017

nested if_else in dplyr mutate_ behaving unexpectedly

My goal is to define a function to convert a numerical variable to a factor variable, with defined levels, and overwrite it in the original dataset using dplyr function mutate.

Following the answer to this previous question I managed to overwrite the variable in the original dataset, but now the nested if_else in the dplyr mutate_ is behaving unexpectedly as in the following example:

    library(dplyr)
    library(lazyeval)


    set.seed(1234)
    a<-runif(10,1,13)
    b<-1:10
    data<-data.frame(cbind(a,b))
    data #original data
    #            a  b
    # 1   2.364441  1
    # 2   8.467593  2
    # 3   8.311297  3
    # 4   8.480553  4
    # 5  11.330985  5
    # 6   8.683727  6
    # 7   1.113949  7
    # 8   3.790606  8
    # 9   8.993005  9
    # 10  7.171014 10

    dataout1 <- mutate(data, a=factor(if_else(a<3,"low",if_else(a>3&a<6,"average","high"))))
    dataout1  # what I expect
    #          a  b
    # 1      low  1
    # 2     high  2
    # 3     high  3
    # 4     high  4
    # 5     high  5
    # 6     high  6
    # 7      low  7
    # 8  average  8
    # 9     high  9
    # 10    high 10

    #my function
    my_func<-function(datain,var,colname=eval(deparse(substitute(var)))){
      dataout <- datain %>% mutate_(.dots=setNames(list(interp(~factor(if_else(var<3,"low",
                                                                                  if_else(var>3&var<6
                                                                                         ,"average",
                                                                                          "high"))),
                                                              var=colname)), 
                                                colname)
                         )
    }

    dataout2<-my_func(data,a)
    dataout2 
      #       a  b
      # 1  high  1
      # 2  high  2
      # 3  high  3
      # 4  high  4
      # 5  high  5
      # 6  high  6
      # 7  high  7
      # 8  high  8
      # 9  high  9
      # 10 high 10

Aucun commentaire:

Enregistrer un commentaire