strengejacke / sjlabelled

Working with Labelled Data in R

Home Page:https://strengejacke.github.io/sjlabelled

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

var_labels attaching labels to wrong variables

andresimi opened this issue · comments

Hi, i noticed that when we try to set a variable name to a non-existing variable,
var_labels continue and set a label to wrong variables from that point on. If it is possible, it should keep the advice, but ignore the non-exstent one.
Thank you!

library(dplyr); library(sjlabelled)

# seting variable names (ok)
mtcars <- mtcars %>% as_tibble() 

mtcars %>%
  var_labels(mpg="mpg test", cyl="cyl test", disp="disp test", hp="hp test", drat="drat test",
             wt="wt test", qsec="qsec test", vs="vs test", am="am test", gear="gear test", 
             carb="carb test") %>%
  get_label()
#>         mpg         cyl        disp          hp        drat          wt 
#>  "mpg test"  "cyl test" "disp test"   "hp test" "drat test"   "wt test" 
#>        qsec          vs          am        gear        carb 
#> "qsec test"   "vs test"   "am test" "gear test" "carb test"

# seting variable name to a non-existent variable (nonexist)
mtcars %>%
  as_tibble() %>%
  var_labels(mpg="mpg test", cyl="cyl test", nonexist="NON EXISTENT", disp="disp test", hp="hp test", drat="drat test",
             wt="wt test", qsec="qsec test", vs="vs test", am="am test", gear="gear test", 
             carb="carb test") %>%
  get_label()
#> Warning: Following elements are no valid column names in `x`: nonexist
#>            mpg            cyl           disp             hp           drat 
#>     "mpg test"     "cyl test" "NON EXISTENT"    "disp test"      "hp test" 
#>             wt           qsec             vs             am           gear 
#>    "drat test"      "wt test"    "qsec test"      "vs test"      "am test" 
#>           carb 
#>    "gear test"

Created on 2018-08-25 by the reprex package (v0.2.0).

Thanks, fixed!