Comments (1)
The character class is already present after loading the data and not related to set_labels()
.
bbq <- read.csv("https://raw.githubusercontent.com/frankcsliu/R4surveyresearch/master/BBQ.csv", header = F)
sapply(bbq, class)
#> V1 V2 V3 V4 V5 V6
#> "character" "character" "character" "character" "character" "character"
#> V7 V8 V9 V10 V11 V12
#> "character" "character" "character" "character" "character" "character"
#> V13 V14 V15 V16 V17 V18
#> "character" "character" "character" "character" "character" "character"
#> V19 V20 V21 V22 V23 V24
#> "character" "character" "character" "character" "character" "character"
#> V25 V26 V27 V28 V29 V30
#> "character" "character" "character" "character" "character" "character"
#> V31 V32 V33 V34 V35 V36
#> "character" "character" "character" "character" "character" "character"
#> V37 V38 V39 V40 V41 V42
#> "character" "character" "character" "character" "character" "character"
#> V43 V44 V45 V46 V47 V48
#> "character" "character" "character" "character" "character" "character"
#> V49 V50 V51 V52 V53 V54
#> "character" "character" "character" "character" "character" "character"
#> V55 V56 V57 V58
#> "character" "character" "character" "character"
bbq <- bbq[-1,]
sapply(bbq, class)
#> V1 V2 V3 V4 V5 V6
#> "character" "character" "character" "character" "character" "character"
#> V7 V8 V9 V10 V11 V12
#> "character" "character" "character" "character" "character" "character"
#> V13 V14 V15 V16 V17 V18
#> "character" "character" "character" "character" "character" "character"
#> V19 V20 V21 V22 V23 V24
#> "character" "character" "character" "character" "character" "character"
#> V25 V26 V27 V28 V29 V30
#> "character" "character" "character" "character" "character" "character"
#> V31 V32 V33 V34 V35 V36
#> "character" "character" "character" "character" "character" "character"
#> V37 V38 V39 V40 V41 V42
#> "character" "character" "character" "character" "character" "character"
#> V43 V44 V45 V46 V47 V48
#> "character" "character" "character" "character" "character" "character"
#> V49 V50 V51 V52 V53 V54
#> "character" "character" "character" "character" "character" "character"
#> V55 V56 V57 V58
#> "character" "character" "character" "character"
Created on 2023-04-03 with reprex v2.0.2
The labelled
class requires for character vectors that values and value names of labels are of same class, i.e. in this case "character". You cannot add non-character names/values to a labelled character vector:
haven::labelled(x = c("a", "b", "c"), labels = c("a" = 1, "b" = 2, "c" = 3))
#> Error in `vec_cast_named()`:
#> ! Can't convert `labels` <double> to match type of `x` <character>.
#> Backtrace:
#> ▆
#> 1. ├─haven::labelled(...)
#> 2. │ └─haven:::vec_cast_named(labels, x, x_arg = "labels", to_arg = "x")
#> 3. │ ├─stats::setNames(vec_cast(x, to, ...), names(x))
#> 4. │ └─vctrs::vec_cast(x, to, ...)
#> 5. └─vctrs (local) `<fn>`()
#> 6. └─vctrs::vec_default_cast(...)
#> 7. ├─base::withRestarts(...)
#> 8. │ └─base (local) withOneRestart(expr, restarts[[1L]])
#> 9. │ └─base (local) doWithOneRestart(return(expr), restart)
#> 10. └─vctrs::stop_incompatible_cast(...)
#> 11. └─vctrs::stop_incompatible_type(...)
#> 12. └─vctrs:::stop_incompatible(...)
#> 13. └─vctrs:::stop_vctrs(...)
#> 14. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
haven::labelled(x = c("a", "b", "c"), labels = c(`1` = "a", `2` = "b", `3` = "c"))
#> <labelled<character>[3]>
#> [1] a b c
#>
#> Labels:
#> value label
#> a 1
#> b 2
#> c 3
haven::labelled(x = c("a", "b", "c"), labels = c("a" = "1", "b" = "2", "c" = "3"))
#> <labelled<character>[3]>
#> [1] a b c
#>
#> Labels:
#> value label
#> 1 a
#> 2 b
#> 3 c
Created on 2023-04-03 with reprex v2.0.2
from sjlabelled.
Related Issues (20)
- `as_label` does not work for a variable with on NAs
- as_labeld(data,drop.na=FALSE) changed from version 1.1.3 to version 1.1.6, intended? HOT 1
- as_labelled missing additional classes used by haven
- expecting a sjlabelled cheat sheet
- Please consider set F as default for as.num and atomic.to.fac HOT 2
- label_to_colnames additional arguments for variable selection HOT 2
- sjlabelled::write_spss(): Codes change from R to SPSS HOT 1
- write_spss not working with integer vectors HOT 12
- R session aborted when trying to import a .dta file HOT 1
- add `ordered` option to `as_label()` HOT 5
- var_labels() function won't work with x as a variable name
- Suggestion for using formats as an attribute
- custom attributes for values and variables HOT 1
- Duplicate value labels in `tab_stackfreq` when using `sjlabelled::as_label(keep.labels = TRUE)` HOT 1
- Problem with tidy-select where() in add-labels()
- how to recode different 'types' of missing in R from a .dta file
- Variable Labels and Date Columns
- set_labels() not working HOT 1
- Label shift after converting incomplete labeled data to factor and afterwards to label
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 sjlabelled.