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

to_factor not working as expected? #44

Closed
aniruhil opened this issue Jan 24, 2017 · 3 comments
Closed

to_factor not working as expected? #44

aniruhil opened this issue Jan 24, 2017 · 3 comments

Comments

@aniruhil
Copy link

aniruhil commented Jan 24, 2017

Starting with a clean R session, running RStudio 1.1.39 and R 3.3.2 on OS X Sierra, I was playing with to_factor() but the output differs from that expected. What could be going on here? I also reinstalled sjmisc via devtools so can't be a package version issue.

> library(sjmisc)
> data(efc)
> str(efc$e42dep)

atomic [1:908] 3 3 3 4 4 4 4 4 4 4 ...

  • attr(*, "label")= chr "elder's dependency"
  • attr(, "labels")= Named num [1:4] 1 2 3 4
    ..- attr(
    , "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"

> table(to_factor(efc$e42dep))

1 2 3 4
66 225 306 304

> barplot(table(to_factor(efc$e42dep)))

plot

The code below works though with the output in line with the documentation:

library(sjPlot)
efc$c172code <- to_factor(efc$c172code)
fit <- lm(barthtot ~ c160age + c12hour + c172code + c161sex, data = efc)
sjt.lm(fit, group.pred = TRUE)

@sjPlot
Copy link
Collaborator

sjPlot commented Jan 24, 2017

What would you expect as output? :-)

to_factor() converts a numeric vector into a factor with numeric levels, but keeps the value and variable labels (which get lost when using as.factor()). If you want the value labels set as factor levels, use to_label().

@aniruhil
Copy link
Author

ah thanks, I messed up with using to_factor() instead of to_label(). love the package; many thanks!

@sjPlot
Copy link
Collaborator

sjPlot commented Jan 24, 2017

@sjPlot sjPlot closed this as completed Jan 24, 2017
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