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

as_factor returning labels with both numerical code and value labels #82

Closed
larmarange opened this Issue Jun 23, 2015 · 7 comments

Comments

Projects
None yet
3 participants
@larmarange
Copy link
Contributor

larmarange commented Jun 23, 2015

When working with labelled data, it's often very useful to have a feedback with both numerical code and value labels.

For example, I have a custom lfactor function (cf. https://github.com/larmarange/JLutils/blob/master/R/label_freq.R) with an option add.codes to return a factor with labels looking like:

  • [1] Woman
  • [2] Man

instead of

  • Man
  • Woman

In fact, seeing both numerical codes and value labels is useful when developing an analysis while for the final results we usually wants only the value labels.

PS: a more general question. It seems that several persons (@sjPlot for example) are starting to develop functions to deal with labelled variables. Do you have any plan to develop a dedicated package where such functions could be integrated?

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jun 23, 2015

Maybe something like this?

s1 <- labelled(c("M", "M", "F"), c(Male = "M", Female = "F"))
as_factor(s1)
as_factor(s1, labels = "values")
#> [1] M M F
#> Levels: M F
as_factor(s1, labels = "labels")
#> [1] Male   Male   Female
#> Levels: Male Female
as_factor(s1, labels = "labels")
#> [1] [M] Male   [M] Male   [F] Female
#> Levels: [M] Male, [F]Female
@sjPlot

This comment has been minimized.

Copy link

sjPlot commented Jun 23, 2015

I guess he was asking for values that are associated with value labels. See for instance:

atomic [1:908] 3 3 3 4 4 4 4 4 4 4 ...
- attr(*, "label")= chr "how dependent is the elder? - subjective perception of carer"
- attr(*, "labels")= Named num [1:4] 1 2 3 4
 ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"

so you have names(attr(x, "labels")) for "independent" "slightly dependent" "moderately dependent" "severely dependent", as.vector(attr(efc$e42dep, "labels")) for 1, 2, 3 and 4, and he was asking for something that combines both, like "get values and labels":

[1] independent
[2] slightly dependent
[3] moderately dependent
[4] severely dependent

@hadley

This comment has been minimized.

Copy link
Member

hadley commented Jun 23, 2015

@sjPlot is that not what I proposed? I don't see the difference

@larmarange

This comment has been minimized.

Copy link
Contributor

larmarange commented Jun 23, 2015

In your example you proposed twice as_factor(s1, labels = "labels") with two different outputs.

I guess that the third example would be something like as_factor(s1, labels = "values_and_labels") or as_factor(s1, labels = "both")

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 31, 2016

Oops, yes, the second one should have been labels = "both"

@hadley hadley closed this in cbb7306 May 31, 2016

@hadley

This comment has been minimized.

Copy link
Member

hadley commented May 31, 2016

Please let me know how the new type = "both" works for you.

One thing I've realised is that factor levels containing spaces don't work particularly well with the default print method for factors:

as_factor(labelled(1:5, c(Bad = 1, Good = 5)), "both")
#> [1] [1] Bad  2        3        4        [5] Good
#> Levels: [1] Bad 2 3 4 [5] Good

But it's not obvious if there's anything I can do here, except maybe suggest an enhancement to base R.

@larmarange

This comment has been minimized.

Copy link
Contributor

larmarange commented May 31, 2016

An option for values who don't have a label is to return [2] 2 instead of just 2.

@lock lock bot locked and limited conversation to collaborators Jun 27, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.