New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior when creating labelled variables in data frames #20

Closed
mbojan opened this Issue Dec 4, 2017 · 9 comments

Comments

Projects
None yet
2 participants
@mbojan
Contributor

mbojan commented Dec 4, 2017

I want to create variable foo but x is created (with value labels):

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data.frame(
  foo = labelled::labelled(1:5, c(a=1, b=2))
) %>%
  str()
#> 'data.frame':    5 obs. of  1 variable:
#>  $ x:Class 'labelled'  atomic [1:5] 1 2 3 4 5
#>   .. ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. .. ..- attr(*, "names")= chr [1:2] "a" "b"

But for tibbles it is OK:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
tibble(
  foo = labelled::labelled(1:5, c(a=1, b=2))
) %>%
  str()
#> Classes 'tbl_df', 'tbl' and 'data.frame':    5 obs. of  1 variable:
#>  $ foo:Class 'labelled'  atomic [1:5] 1 2 3 4 5
#>   .. ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. .. ..- attr(*, "names")= chr [1:2] "a" "b"

I'm not yet sure why it happens.

@larmarange

This comment has been minimized.

Show comment
Hide comment
@larmarange

larmarange Dec 4, 2017

Owner
Owner

larmarange commented Dec 4, 2017

@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 4, 2017

Contributor

Do you mean

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
l <- labelled::labelled(1:5, c(a=1, b=2))
data.frame(
  foo = l
) %>%
  str()
#> 'data.frame':    5 obs. of  1 variable:
#>  $ x:Class 'labelled'  atomic [1:5] 1 2 3 4 5
#>   .. ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. .. ..- attr(*, "names")= chr [1:2] "a" "b"
Contributor

mbojan commented Dec 4, 2017

Do you mean

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
l <- labelled::labelled(1:5, c(a=1, b=2))
data.frame(
  foo = l
) %>%
  str()
#> 'data.frame':    5 obs. of  1 variable:
#>  $ x:Class 'labelled'  atomic [1:5] 1 2 3 4 5
#>   .. ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. .. ..- attr(*, "names")= chr [1:2] "a" "b"
@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 4, 2017

Contributor

So I think this is because

data.frame converts each of its arguments to a data frame by calling as.data.frame(optional = TRUE)

and

l <- labelled::labelled(1:5, c(a=1, b=2))
as.data.frame(l)
#>   x
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
Contributor

mbojan commented Dec 4, 2017

So I think this is because

data.frame converts each of its arguments to a data frame by calling as.data.frame(optional = TRUE)

and

l <- labelled::labelled(1:5, c(a=1, b=2))
as.data.frame(l)
#>   x
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 4, 2017

Contributor

OK, I think I've fixed it. Stand by for a PR.

Contributor

mbojan commented Dec 4, 2017

OK, I think I've fixed it. Stand by for a PR.

mbojan added a commit to mbojan/labelled that referenced this issue Dec 4, 2017

as.data.frame.labelled passes ... further
to as.data.frame.* methods.
References larmarange#20

mbojan added a commit to mbojan/labelled that referenced this issue Dec 4, 2017

larmarange added a commit that referenced this issue Dec 4, 2017

as.data.frame.labelled passes ... further
to as.data.frame.* methods.
References #20

larmarange added a commit that referenced this issue Dec 4, 2017

@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 4, 2017

Contributor

Close?

Contributor

mbojan commented Dec 4, 2017

Close?

@larmarange

This comment has been minimized.

Show comment
Hide comment
@larmarange

larmarange Dec 4, 2017

Owner
Owner

larmarange commented Dec 4, 2017

@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 4, 2017

Contributor

OK, sure. Thanks.

Contributor

mbojan commented Dec 4, 2017

OK, sure. Thanks.

@larmarange larmarange closed this Dec 19, 2017

@mbojan

This comment has been minimized.

Show comment
Hide comment
@mbojan

mbojan Dec 19, 2017

Contributor

Thanks for that. A note of caution: I recall including ł in my name in DESCRIPTION caused some hiccups with package checks and CRAN submissions in the past. Perhaps it is OK now, but if you run into this, feel free to make it just "Michal".

Contributor

mbojan commented Dec 19, 2017

Thanks for that. A note of caution: I recall including ł in my name in DESCRIPTION caused some hiccups with package checks and CRAN submissions in the past. Perhaps it is OK now, but if you run into this, feel free to make it just "Michal".

@larmarange

This comment has been minimized.

Show comment
Hide comment
@larmarange

larmarange Dec 19, 2017

Owner
Owner

larmarange commented Dec 19, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment