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

Pivot wider fails when the class of variable is a vector longer than one. #780

Closed
jwilliman opened this issue Oct 17, 2019 · 3 comments
Closed

Comments

@jwilliman
Copy link

When running pivot_wider over data where one of the character variables is labelled using the Hmisc::label function, I get the warning message Error: Only bare vectors have shapes.

The Hmisc::label function not only adds a label attribute to the variable, but it changes the variable class to c("labelled", "character").

I found that I could reproduce this error by adding to the class of any variable prior to using pivot_wider. Interestingly, this doesn't occur for factors - so I'm guessing factor variables are treated differently?

I'm not sure if editing the class of a variable is peculiar to Hmisc (and should be corrected there), or whether it should be considered acceptable in general and thus catered for?

## This works
data("fish_encounters")
fish_encounters$fish <- as.character(fish_encounters$fish)
fish_encounters %>% pivot_wider(names_from = station, values_from = seen)

## So does this
data("fish_encounters")
class(fish_encounters$fish) <- c("factor", "extra")
fish_encounters %>% pivot_wider(names_from = station, values_from = seen)

## But these all fail
data("fish_encounters")
fish_encounters$fish <- as.character(fish_encounters$fish)
class(fish_encounters$fish) <- c("character", "extra")
fish_encounters %>% pivot_wider(names_from = station, values_from = seen)

data("fish_encounters")
fish_encounters$station<- as.character(fish_encounters$station)
class(fish_encounters$station) <- c("character", "extra")
fish_encounters %>% pivot_wider(names_from = station, values_from = seen)

data("fish_encounters")
class(fish_encounters$seen) <- c("integer", "extra")
fish_encounters %>% pivot_wider(names_from = station, values_from = seen)
@hadley
Copy link
Member

hadley commented Oct 18, 2019

Could you please rework your reproducible example to use the reprex package ? That makes it easier to see both the input and the output, formatted in such a way that I can easily re-run in a local session.

@jeffreypullin
Copy link
Contributor

Minimal reprex:

x <- 1:10
Hmisc::label(x) <- "x's label"
vctrs:::shape(x)
#> Only bare vectors have shapes.

Created on 2019-10-19 by the reprex package (v0.3.0)

So this is a duplicate of #773 and hence r-lib/vctrs#614.

@hadley
Copy link
Member

hadley commented Oct 19, 2019

Thanks for the investigation @jeffreypullin!

@hadley hadley closed this as completed Oct 19, 2019
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

No branches or pull requests

3 participants