Comments (4)
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.
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.
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)
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.
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)
- Rec bug with "lo" characters
- select_helpers not working HOT 1
- Important bug in Rec HOT 2
- the speed of "rec" function slow when I use mutate with large dataset
- `move_columns` does not work when passed a variable in .after HOT 1
- rec and space strings
- Frequency of characters coercible as numerics
- Use str_contains for dataframe? HOT 2
- merge_imputations only providing a subset of variables
- Merges multiple imputed data frames into a single data frame HOT 3
- row_means() proportion of datapoints HOT 2
- FR: allow more printing formatting for frq HOT 1
- weights argument fails when using dplyr group_by
- frq fails when used for more than 1 variable and after `dplyr::group_by`
- Does not merge when data are in long format HOT 1
- Feature request: use -c syntax for descr "show" argument
- Issue with weighted percentage in `flat_table()`
- Issue with rec() else=copy not passing old labels
- sjmisc::rec() - val.labels function is not working
- sjmisc::rec generated factors not in the expected order
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sjmisc.