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_longer gives incorrect output when pivoting database column named "name", but gives expected output on local data frame #692

Closed
eipi10 opened this issue Aug 10, 2021 · 0 comments · Fixed by #695

Comments

@eipi10
Copy link
Contributor

eipi10 commented Aug 10, 2021

When a column in a database is called name, running pivot_longer() on that column results in all of its values being replaced by "name" in the value column of the long data. On the other hand, when a column in a local data frame is called name, running pivot_longer() gives the expected behavior.

library(tidyverse)
library(dbplyr)
#> 
#> Attaching package: 'dbplyr'
#> The following objects are masked from 'package:dplyr':
#> 
#>     ident, sql

# Example with a database
db = memdb_frame(name=LETTERS[1:4], id=as.character(1:4), group=rep(c("x","y"), each=2))
db
#> # Source:   table<dbplyr_001> [?? x 3]
#> # Database: sqlite 3.35.5 [:memory:]
#>   name  id    group
#>   <chr> <chr> <chr>
#> 1 A     1     x    
#> 2 B     2     x    
#> 3 C     3     y    
#> 4 D     4     y

# All values in name column get replaced with "name" in the long value column
db %>% pivot_longer(c(name, id))
#> # Source:   lazy query [?? x 3]
#> # Database: sqlite 3.35.5 [:memory:]
#>   group name  value
#>   <chr> <chr> <chr>
#> 1 x     name  name 
#> 2 x     name  name 
#> 3 y     name  name 
#> 4 y     name  name 
#> 5 x     id    1    
#> 6 x     id    2    
#> 7 y     id    3    
#> 8 y     id    4

# Workaround to get desired output
db %>% rename(nm=name) %>% pivot_longer(c(nm, id)) 
#> # Source:   lazy query [?? x 3]
#> # Database: sqlite 3.35.5 [:memory:]
#>   group name  value
#>   <chr> <chr> <chr>
#> 1 x     nm    A    
#> 2 x     nm    B    
#> 3 y     nm    C    
#> 4 y     nm    D    
#> 5 x     id    1    
#> 6 x     id    2    
#> 7 y     id    3    
#> 8 y     id    4

# Another workaround to get desired output
db %>% pivot_longer(c(name, id), names_to="nm")
#> # Source:   lazy query [?? x 3]
#> # Database: sqlite 3.35.5 [:memory:]
#>   group nm    value
#>   <chr> <chr> <chr>
#> 1 x     name  A    
#> 2 x     name  B    
#> 3 y     name  C    
#> 4 y     name  D    
#> 5 x     id    1    
#> 6 x     id    2    
#> 7 y     id    3    
#> 8 y     id    4

# Example with a local data frame
d = tibble(name=LETTERS[1:4], id=as.character(1:4), group=rep(c("x","y"), each=2))
d
#> # A tibble: 4 × 3
#>   name  id    group
#>   <chr> <chr> <chr>
#> 1 A     1     x    
#> 2 B     2     x    
#> 3 C     3     y    
#> 4 D     4     y

# Desired output
d %>% pivot_longer(c(name, id))
#> # A tibble: 8 × 3
#>   group name  value
#>   <chr> <chr> <chr>
#> 1 x     name  A    
#> 2 x     id    1    
#> 3 x     name  B    
#> 4 x     id    2    
#> 5 y     name  C    
#> 6 y     id    3    
#> 7 y     name  D    
#> 8 y     id    4

Created on 2021-08-10 by the reprex package (v2.0.1)

@hadley hadley transferred this issue from tidyverse/tidyr Aug 16, 2021
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 a pull request may close this issue.

1 participant