-
Notifications
You must be signed in to change notification settings - Fork 185
Closed
Description
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 4Created on 2021-08-10 by the reprex package (v2.0.1)
Metadata
Metadata
Assignees
Labels
No labels