Coder Social home page Coder Social logo

different behavior for character vs. numeric data, when using named vector for labels argument of set_labels()? about sjlabelled HOT 5 CLOSED

strengejacke avatar strengejacke commented on July 29, 2024
different behavior for character vs. numeric data, when using named vector for labels argument of set_labels()?

from sjlabelled.

Comments (5)

strengejacke avatar strengejacke commented on July 29, 2024 1

I just submitted an update to CRAN, and this resolved issue should be in the official CRAN release.

from sjlabelled.

jmobrien avatar jmobrien commented on July 29, 2024

Just following up about this after a while. I'd like to use the tool more in my workflow, but as it stands I'm just having to set things up manually.

I still don't understand the seemingly inconsistent behavior when labeling numeric vs. character data types. Looking at the code this behavior appears to be intentional, with the linked section doing the flip if things match (i.e., string data is given string labels).

If there's a mismatch because the labels are given numeric elements with string names, it ignores those and throws a warning, like so:

  example <- sample(c("one", "two", "three", "four"), 40, replace = TRUE)
  example <- set_labels(dummy, labels = c("one" = 1 , "two" = 2, "three" = 3 , "four" = 3))

  example   # unlabelled               

However, a labels argument structured like the above would already be improper based on the guidance in the help docs for set_labels.

In fact, it even looks like a similar mistake would be fixed if it were made when labeling a numeric vector, i.e. here

Is there a purpose to this behavior I'm not understanding? Perhaps something to do with Haven, etc.?

from sjlabelled.

strengejacke avatar strengejacke commented on July 29, 2024

I think there must have been a reason to do this, but I can't remember. I changed the behaviour, so it is now in line with the default haven behaviour:

charvec <- sjlabelled::set_labels(
  c("one", "two", "three", "four"), 
  labels = c(a = "one", b = "two", c = "three", d = "four")
)

charvec2 <-  haven::labelled(
  c("one", "two", "three", "four"), 
  labels = c(a = "one", b = "two", c = "three", d = "four")
)

charvec
#> [1] "one"   "two"   "three" "four" 
#> attr(,"labels")
#>       a       b       c       d 
#>   "one"   "two" "three"  "four"
charvec2
#> <labelled<character>[4]>
#> [1] one   two   three four 
#> 
#> Labels:
#>  value label
#>    one     a
#>    two     b
#>  three     c
#>   four     d

sjlabelled::get_labels(charvec, value = "p")
#> [1] "[four] d"  "[one] a"   "[three] c" "[two] b"
sjlabelled::get_labels(charvec2, value = "p")
#> [1] "[four] d"  "[one] a"   "[three] c" "[two] b"

Created on 2021-05-11 by the reprex package (v2.0.0)

from sjlabelled.

jmobrien avatar jmobrien commented on July 29, 2024

Excellent, thanks for your help on this.

And thanks for all your work on this package overall!

from sjlabelled.

jmobrien avatar jmobrien commented on July 29, 2024

Just a small follow-up on this--Is there a plan to release a new version of the package CRAN any time soon? Just asking so I can plan--much of my work is done on a managed server, and the policy is generally only to use official CRAN packages.

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.