Coder Social home page Coder Social logo

Comments (1)

strengejacke avatar strengejacke commented on July 29, 2024

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)

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.