Code Monkey home page Code Monkey logo

Comments (4)

sjPlot avatar sjPlot commented on September 14, 2024

into a factor using the values of the variable as the labels of the factor

No, this is what to_label does. to_factor converts a numeric into a numeric factor (i.e. with numeric factor levels), and keeps label attributes (as opposed to as.factor, which drops all label attributes). I needed this function to convert SPSS-imported data (where variables are numerical / atomic) into (numeric) factors, but keeping the label attributes. This is one of the great features of the sjPlot package, to automatically label plots and tables, based on label attributes.

btw., I have added many features to the package again, will commit them tonight. Many functions now deal with non-labelled values, or missing code values (is_na attributes).

Some examples:

library(haven)
library(sjmisc)

test <- labelled(c(1,2,3,4,5,1,2,5,4), c(Bad = 1, Good = 5))
as_factor(test)
> [1] Bad  <NA> <NA> <NA> Good Bad  <NA> Good <NA>
> Levels: Bad Good

to_label(test)
> [1] Bad  <NA> <NA> <NA> Good Bad  <NA> Good <NA>
> Levels: Bad Good

to_label(test, add.non.labelled = TRUE)
> [1] Bad  2    3    4    Good Bad  2    Good 4   
> Levels: Bad Good 2 3 4

x <- labelled(c(1, 2, 1, 3, 4, 1),
              c(Male = 1, Female = 2, Refused = 3, "N/A" = 4),
              c(FALSE, FALSE, TRUE, TRUE))
# to labelled factor, with missing labels
to_label(x)
> [1] Male    Female  Male    Refused N/A     Male   
> Levels: Male Female Refused N/A

# to labelled factor, missings removed
to_label(x, drop.na = TRUE)
> [1] Male   Female Male   <NA>   <NA>   Male  
> Levels: Male Female

# to factor, with missing labels
to_factor(x)
> [1] 1 2 1 3 4 1
> attr(,"labels")
>    Male  Female Refused     N/A 
>       1       2       3       4 
> attr(,"is_na")
> [1] FALSE FALSE  TRUE  TRUE
> Levels: 1 2 3 4

# to factor, missings removed
to_factor(x, drop.na = TRUE)
> [1] 1    2    1    <NA> <NA> 1   
> attr(,"labels")
>   Male Female 
>      1      2 
> attr(,"is_na")
> [1] FALSE FALSE
> Levels: 1 2

from sjmisc.

sjPlot avatar sjPlot commented on September 14, 2024

Ok, I now also fixed the correct order for non-labelled values:

get_labels(test, include.non.labelled = T, include.values = "p")
> [1] "[1] Bad"  "[2] 2"    "[3] 3"    "[4] 4"    "[5] Good"

to_label(test, add.non.labelled = T)
> [1] Bad 2   3   4   2   Bad 2   2   4  
> Levels: Bad 2 3 4 Good

to_factor(test)
> [1] 1 2 3 4 5 1 2 5 4
> attr(,"labels")
>  Bad    2    3    4 Good 
>    1    2    3    4    5 
> attr(,"is_na")
> [1] FALSE FALSE FALSE FALSE FALSE
> Levels: 1 2 3 4 5

from sjmisc.

sjPlot avatar sjPlot commented on September 14, 2024

And a final example of your initial request:

library(sjPlot)
library(sjmisc)
data(efc)
fit <- lm(tot_sc_e ~ c12hour + c160age + e42dep + neg_c_7 + c172code, data = efc)
sjp.lm(fit)

rplot

As you can see, the left axis is labelled automatically. Same applies to tables, e.g. sjt.lm, section Automatic grouping of categorical predictors: labels for factor name and factor levels are automatically set.

from sjmisc.

larmarange avatar larmarange commented on September 14, 2024

OK thanks for the precision

I will try to start a draf of a labelled packages in the following days (depending on my available time). The purpose will be to deal with and only with manipulation of labelled data. I will take time to go through your code carefully and will implement some tests.

I will let you know once I will have something relatively advanced.

Regards

from sjmisc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.