Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recode() not adding value labels correctly #73

Closed
xikec opened this issue Nov 13, 2020 · 3 comments
Closed

recode() not adding value labels correctly #73

xikec opened this issue Nov 13, 2020 · 3 comments

Comments

@xikec
Copy link

xikec commented Nov 13, 2020

I used recode() to both recode and add value labels at the same time. I realized some value labels were missing in certain variables.

Below is an example. The responses are 1, 4, 5, 6, 7. I want to recode them 1 to 5, 4 to 4, 5 to 3, 6 to 2, and 7 to 1 with value labels 5 = Always, 4 = Often, and so on.

df1 <- data.frame("Q1" = sample(c(1,7), 50, replace = T),
                  "Q2" = sample(c(1,4,5), 50, replace = T), 
                  "Q3" = sample(c(4,5), 50, replace = T), 
                  "Q4" = sample(c(1,4,5,6), 50, replace = T),
                  "Q5" = sample(c(1,4,6), 50, replace = T), 
                  "Q6" = sample(c(4,5,7,NA), 50, replace = T),
                  "Q7" = sample(c(5,NA), 50, replace = T), 
                  "Q8" = sample(c(1,4,5,6,7), 50, replace = T))

df2 <- expss::recode(df1[1:length(df1)], "Always" = 1 ~ 5, "Often" = 4 ~ 4, "Sometimes" = 5 ~ 3, "Rarely" = 6 ~ 2, "Never" = 7 ~ 1, T ~ copy)

The following result of val_lab() shows that 1 = Never is missing in Q2 and Q3 even when Q2 does have values of 1. And Q7, with only the values of 5 and NA, is applied with the correct value labels.

> sapply(df2, val_lab)
$Q1
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

$Q2
   Rarely Sometimes     Often    Always 
        2         3         4         5 

$Q3
   Rarely Sometimes     Often    Always 
        2         3         4         5 

$Q4
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

$Q5
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

$Q6
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

$Q7
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

$Q8
    Never    Rarely Sometimes     Often    Always 
        1         2         3         4         5 

If I used a different approach: Using recode() to first recode the values, then num_lab() and val_lab() to assign the value labels, everything worked as planned.

df3 <- expss::recode(df1[1:length(df1)], 1 ~ 5, 4 ~ 4, 5 ~ 3, 6 ~ 2, 7 ~ 1, T ~ copy)

val_lab(df3) <- num_lab("1 Never
                         2 Rarely
                         3 Sometimes
                         4 Often
                         5 Always")

The result is

> sapply(df3, val_lab)
          Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8
Never      1  1  1  1  1  1  1  1
Rarely     2  2  2  2  2  2  2  2
Sometimes  3  3  3  3  3  3  3  3
Often      4  4  4  4  4  4  4  4
Always     5  5  5  5  5  5  5  5

As I haven't seen anyone posted this issue on github or stackoverflow, I am assuming I must have missed something. Please help. Thanks!

@gdemin
Copy link
Owner

gdemin commented Nov 13, 2020

Hi,
Thank you for detailed report. I reproduced the issue and will try to fix it in the next release.

@gdemin
Copy link
Owner

gdemin commented Nov 16, 2020

Fixed in 0.10.7

@gdemin gdemin closed this as completed Nov 16, 2020
@xikec
Copy link
Author

xikec commented Nov 16, 2020

It worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants