set_labels() not working
frankcsliu opened this issue · comments
frankcsliu commented
I just found that it did not work as expected (no labels are added to a variable).
data for replication the issue: https://ishort.ink/DT1w
bbq$V2 <- set_labels(bbq$V2, labels=c("yes", "no"))
table(bbq$V2) #no labels were added
Please help check this issue. Thank you.
Daniel commented
It works, it's just that table()
ignores the label-attributes.
If you want to use value labels as factor levels, use as_label()
:
load("d:/Downloads/BBQ.rda")
bbq$V2 <- sjlabelled::set_labels(bbq$V2, labels = c("yes", "no"))
str(bbq$V2)
#> int [1:650] 1 1 1 2 1 2 1 1 1 1 ...
#> - attr(*, "label")= Named chr "今年的中秋節您有與家人團聚嗎?(延後到國慶連假也算)"
#> ..- attr(*, "names")= chr "V2"
#> - attr(*, "labels")= Named num [1:2] 1 2
#> ..- attr(*, "names")= chr [1:2] "yes" "no"
sjmisc::frq(bbq$V2)
#> 今年的中秋節您有與家人團聚嗎?(延後到國慶連假也算) (x) <integer>
#> # total N=650 valid N=650 mean=1.17 sd=0.37
#>
#> Value | Label | N | Raw % | Valid % | Cum. %
#> ----------------------------------------------
#> 1 | yes | 541 | 83.23 | 83.23 | 83.23
#> 2 | no | 109 | 16.77 | 16.77 | 100.00
#> <NA> | <NA> | 0 | 0.00 | <NA> | <NA>
table(sjlabelled::as_label(bbq$V2))
#>
#> yes no
#> 541 109
Created on 2023-04-02 with reprex v2.0.2