-
Notifications
You must be signed in to change notification settings - Fork 417
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 with duplicate column names #472
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Also need to test with |
This comment has been minimized.
This comment has been minimized.
Possibly related: test_that("minimal spec works", {
df <- tibble(x = "x", y = 1)
sp <- tibble::tribble(
~.name, ~.value,
"x", "X",
"y", "X",
)
pv <- pivot_long(df, spec = sp)
expect_named(pv, c("X"))
expect_equal(pv$X, c("x", "y"))
}) |
Reprex update for latest interface: library(dplyr, warn.conflicts = FALSE)
library(tidyr)
d <- data.frame(c("A", "B"), rbind(c(0, 0, 5, 5), c(0, 0, 10, 10)))
colnames(d) <- c("Group", "Var1", "Var2", "Var1", "Var2")
d <- as_tibble(d, .name_repair = "minimal")
d
#> # A tibble: 2 x 5
#> Group Var1 Var2 Var1 Var2
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 A 0 0 5 5
#> 2 B 0 0 10 10
d %>% pivot_long(-Group)
#> # A tibble: 4 x 3
#> Group name value
#> <fct> <chr> <dbl>
#> 1 A Var1 0
#> 2 A Var2 0
#> 3 B Var1 0
#> 4 B Var2 0 Created on 2019-03-06 by the reprex package (v0.2.1.9000) |
After thinking about it for a while, I think the best approach is to make the copies explicit. This makes it straightforward to implement, makes it obvious from the outside, and gives you all the needed data. # A tibble: 8 x 4
Group name .copy value
<fct> <chr> <int> <dbl>
1 A Var1 1 0
2 A Var2 1 0
3 A Var1 2 5
4 A Var2 2 5
5 B Var1 1 0
6 B Var2 1 0
7 B Var1 2 10
8 B Var2 2 10
Warning message:
Duplicate column names detected, adding .copy variable |
That would be a very nice solution indeed! Thanks !!! |
I'm more and more often confronted with messy spreadsheets with duplicate column names that need to be tidied up as for example in this SO question.
tidyr::gather
simply refuse to do it (error message) andreshape::melt
prreshape2::melt
return the wrong numbers without any warning. Thedata.table
version ofmelt
works as intended.Created on 2018-06-25 by the reprex package (v0.2.0).
The text was updated successfully, but these errors were encountered: