strengejacke / sjlabelled

Working with Labelled Data in R
https://strengejacke.github.io/sjlabelled
86 stars 11 forks source link

if label is numeric, prevents replacement recycling #26

Closed daranzolin closed 5 years ago

daranzolin commented 5 years ago

If the label is also a number, as_label will continue updating the value. Here's what was happening to us:

as_label_pr

strengejacke commented 5 years ago

Thanks! Do you have a small example, which case is addressed by your PR?

daranzolin commented 5 years ago

Yes, my apologies for the missing regex--I was previously in a hurry. In the example below, the initial label for x[1] is 10. But as far as I can tell, as_label replaces x[1] as 10, and then replaces 10 with the next corresponding label. My hacky PR makes the replacement for x[1] unique, so it won't be replaced again in the for loop.

library(sjlabelled)
#> Warning: package 'sjlabelled' was built under R version 3.5.2
x <- 1:50
x <- set_labels(x, labels = as.character(10:60))
#> More labels than values of "x". Using first 50 labels.
x
#>  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#> [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#> [47] 47 48 49 50
#> attr(,"labels")
#> 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 
#>  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
#> 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 
#> 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
x <- as_label(x)
x
#>  [1] 55 56 57 58 59 51 52 53 54 55 56 57 58 59 51 52 53 54 55 56 57 58 59
#> [24] 51 52 53 54 55 56 57 58 59 51 52 53 54 55 56 57 58 59 51 52 53 54 55
#> [47] 56 57 58 59
#> 50 Levels: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ... 59

Created on 2019-08-03 by the reprex package (v0.2.1)

strengejacke commented 5 years ago

Got it, thanks! You may add yourself as contributor to the description, if you like.

daranzolin commented 5 years ago

Done! :)

Thanks again for this terrific package--our office uses it extensively.