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

Undesired behaviour with cro_cases function when value labels are defined #3

Closed
epdiamantopoulos opened this issue Aug 23, 2017 · 2 comments
Assignees

Comments

@epdiamantopoulos
Copy link

Congratulations for your efforts on your excelent package.

It seems that cro_cases does not function properly when labels are previously attached to the variables (using val_lab).

Code to reproduce:

gender = as.factor(c(1, 1, 1, 0, 0, 0, 1, 0, 0, 0,
                     1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,
                     0, 0, 1, 0, 1, 1, 1, 1, 1, 0))
group = as.factor(c(rep(1, 10), rep(2, 11), rep(3, 10)))
sample.df = data.frame(group, gender)

library(expss)
sample.df = apply_labels(sample.df,
                     gender = "Gender",
                     group = "Group")

try without value labels: functioning ok:

cro_cases(sample.df$group, sample.df$gender)

screenshot - 23_8_2017 7_52_16

try with value labels: functioning not ok:

val_lab(sample.df$group) = num_lab("1 A
                               2 B
                               3 C")
val_lab(sample.df$gender) = num_lab("0 Women
                                1 Men")
cro_cases(sample.df$group, sample.df$gender)

screenshot - 23_8_2017 7_52_47

The problem appears when value labels are defined at column variable (gender in the above example). Value labels at the row variable as well as presence or not of the labels at the variables are irrelevant.

@gdemin
Copy link
Owner

gdemin commented Aug 23, 2017

Hi! Thank you for your feedback.
I will try to fix this issue in the future version. Generally speaking, value labels on factors give undefined behavior because variables with value labels and factors both have labels and there is no obvious way of combining factor's labels with additional labels applied with val_lab. It's better do not apply value labels on factors in your workflow.
So, your code will be (note the absence of factor):

gender = c(1, 1, 1, 0, 0, 0, 1, 0, 0, 0,
                     1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1,
                     0, 0, 1, 0, 1, 1, 1, 1, 1, 0)
group = c(rep(1, 10), rep(2, 11), rep(3, 10))
sample.df = data.frame(group, gender)

library(expss)
sample.df = apply_labels(sample.df,
                         gender = "Gender",
                         group = "Group")

val_lab(sample.df$group) = num_lab("1 A
                               2 B
                               3 C")
val_lab(sample.df$gender) = num_lab("0 Women
                                1 Men")
cro_cases(sample.df$group, sample.df$gender)

@gdemin gdemin self-assigned this Aug 23, 2017
@epdiamantopoulos
Copy link
Author

epdiamantopoulos commented Aug 23, 2017 via email

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