Skip to content
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

data_to_long() did not work with labelled data frames #498

Merged
merged 1 commit into from
May 7, 2024

Conversation

strengejacke
Copy link
Member

No description provided.

Copy link
Member Author

@strengejacke strengejacke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem:

data(efc, package = "datawizard")
x1 <- datawizard::data_filter(efc, 1:10)
x2 <- datawizard::data_filter(efc, 21:40)

str(x1)
#> 'data.frame':    10 obs. of  5 variables:
#>  $ c12hour : num  16 148 70 NA 168 16 161 110 28 40
#>   ..- attr(*, "label")= chr "average number of hours of care per week"
#>  $ e16sex  : num  2 2 2 2 2 2 1 2 2 2
#>   ..- attr(*, "label")= chr "elder's gender"
#>   ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. ..- attr(*, "names")= chr [1:2] "male" "female"
#>  $ e42dep  : Factor w/ 4 levels "1","2","3","4": 3 3 3 NA 4 4 4 4 4 4
#>   ..- attr(*, "label")= chr "elder's dependency"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  $ c172code: num  2 2 1 2 2 2 2 2 NA 2
#>   ..- attr(*, "label")= chr "carer's level of education"
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 3
#>   .. ..- attr(*, "names")= chr [1:3] "low level of education" "intermediate level of education" "high level of education"
#>  $ neg_c_7 : num  12 20 11 10 12 19 15 11 15 10
#>   ..- attr(*, "label")= chr "Negative impact with 7 items"
str(x2)
#> 'data.frame':    20 obs. of  5 variables:
#>  $ c12hour : num  150 50 18 168 15 168 12 7 35 168 ...
#>   ..- attr(*, "label")= chr "average number of hours of care per week"
#>  $ e16sex  : num  2 1 2 2 2 1 2 1 2 2 ...
#>   ..- attr(*, "label")= chr "elder's gender"
#>   ..- attr(*, "labels")= Named num [1:2] 1 2
#>   .. ..- attr(*, "names")= chr [1:2] "male" "female"
#>  $ e42dep  : Factor w/ 4 levels "1","2","3","4": 4 3 4 4 3 4 4 2 3 4 ...
#>   ..- attr(*, "label")= chr "elder's dependency"
#>   ..- attr(*, "labels")= Named num [1:4] 1 2 3 4
#>   .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"
#>  $ c172code: num  2 2 2 2 2 2 3 2 2 1 ...
#>   ..- attr(*, "label")= chr "carer's level of education"
#>   ..- attr(*, "labels")= Named num [1:3] 1 2 3
#>   .. ..- attr(*, "names")= chr [1:3] "low level of education" "intermediate level of education" "high level of education"
#>  $ neg_c_7 : num  11 9 8 14 11 23 NA 11 15 11 ...
#>   ..- attr(*, "label")= chr "Negative impact with 7 items"

cbind(x1, x2)
#> Error in data.frame(..., check.names = FALSE): arguments imply differing number of rows: 10, 20

x1 <- efc[1:10, ]
x2 <- efc[21:40, ]

str(x1)
#> 'data.frame':    10 obs. of  5 variables:
#>  $ c12hour : num  16 148 70 NA 168 16 161 110 28 40
#>  $ e16sex  : num  2 2 2 2 2 2 1 2 2 2
#>  $ e42dep  : Factor w/ 4 levels "1","2","3","4": 3 3 3 NA 4 4 4 4 4 4
#>  $ c172code: num  2 2 1 2 2 2 2 2 NA 2
#>  $ neg_c_7 : num  12 20 11 10 12 19 15 11 15 10
str(x2)
#> 'data.frame':    20 obs. of  5 variables:
#>  $ c12hour : num  150 50 18 168 15 168 12 7 35 168 ...
#>  $ e16sex  : num  2 1 2 2 2 1 2 1 2 2 ...
#>  $ e42dep  : Factor w/ 4 levels "1","2","3","4": 4 3 4 4 3 4 4 2 3 4 ...
#>  $ c172code: num  2 2 2 2 2 2 3 2 2 1 ...
#>  $ neg_c_7 : num  11 9 8 14 11 23 NA 11 15 11 ...

cbind(x1, x2)
#> Warning in data.frame(..., check.names = FALSE): row names were found from a
#> short variable and have been discarded
#>    c12hour e16sex e42dep c172code neg_c_7 c12hour e16sex e42dep c172code
#> 1       16      2      3        2      12     150      2      4        2
#> 2      148      2      3        2      20      50      1      3        2
#> 3       70      2      3        1      11      18      2      4        2
#> 4       NA      2   <NA>        2      10     168      2      4        2
#> 5      168      2      4        2      12      15      2      3        2
#> 6       16      2      4        2      19     168      1      4        2
#> 7      161      1      4        2      15      12      2      4        3
#> 8      110      2      4        2      11       7      1      2        2
#> 9       28      2      4       NA      15      35      2      3        2
#> 10      40      2      4        2      10     168      2      4        1
#> 11      16      2      3        2      12     150      1      4        2
#> 12     148      2      3        2      20     168      1      4        1
#> 13      70      2      3        1      11      NA      2   <NA>        2
#> 14      NA      2   <NA>        2      10     119      1      4        2
#> 15     168      2      4        2      12     168      2      4        2
#> 16      16      2      4        2      19     168      1      4        1
#> 17     161      1      4        2      15     168      1      4        2
#> 18     110      2      4        2      11      28      2      2        2
#> 19      28      2      4       NA      15     168      1      4        2
#> 20      40      2      4        2      10      30      2      3        2
#>    neg_c_7
#> 1       11
#> 2        9
#> 3        8
#> 4       14
#> 5       11
#> 6       23
#> 7       NA
#> 8       11
#> 9       15
#> 10      11
#> 11      25
#> 12       9
#> 13      15
#> 14      20
#> 15       9
#> 16      10
#> 17      19
#> 18       8
#> 19      17
#> 20      16

Created on 2024-05-07 with reprex v2.1.0

@strengejacke strengejacke merged commit 54635c8 into main May 7, 2024
24 of 27 checks passed
@strengejacke strengejacke deleted the fix_data_to_long branch May 7, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant