-
Notifications
You must be signed in to change notification settings - Fork 417
Closed
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"pivot rectangular data to different "shapes"
Description
When pivoting a column that would result in invalid names, a custom function can be provided to correct them, both the unnest and pivot_wider function direct to the vctrs::vec_as_names function. The unnest function yields the expected output, while the pivot_wider function yields the original colnames, with duplicates.
pivot_wider with custom function does not apply function results.
In the three blocks below, the colname "test" is duplicated, it is corrected in the first two (vctrs::vec_as_names, unnest) but not in the third (pivot_wider) to "test_attr".
library(magrittr)
library(tidyverse)
vctrs::vec_as_names(
c("test","test2", "test"),
repair =
function(x) {
print(duplicated(x))
x[duplicated(x)]
ifelse(
duplicated(x),
paste0(x, "_attr"),
x
)
}
)
#> [1] FALSE FALSE TRUE
#> [1] "test" "test2" "test_attr"
tribble(
~test, ~data,
"a", tibble(test = 1),
"b", tibble(test1 = 2)
) %>%
unnest(
data,
names_repair =
function(x) {
names = ifelse(
duplicated(x),
paste0(x, "_attr"),
x
)
print(names)
return(names)
}
)
#> [1] "test" "test_attr" "test1"
#> # A tibble: 2 x 3
#> test test_attr test1
#> <chr> <dbl> <dbl>
#> 1 a 1 NA
#> 2 b NA 2
tribble(
~test, ~names, ~values,
"a", "test", 1,
"b", "test2", 2
) %>%
pivot_wider(
names_from = names,
values_from = values,
names_repair =
function(x) {
names = ifelse(
duplicated(x),
paste0(x, "_attr"),
x
)
print(names)
return(names)
}
)
#> [1] "test" "test_attr" "test2"
#> # A tibble: 2 x 3
#> test test test2
#> <chr> <chr> <dbl>
#> 1 a a NA
#> 2 b b 2
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252
[3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C
[5] LC_TIME=English_Canada.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyr_1.1.3 stringr_1.4.0 purrr_0.3.4 dplyr_1.0.5
[5] tibble_3.1.0 readr_1.4.0 magrittr_2.0.1
Metadata
Metadata
Assignees
Labels
bugan unexpected problem or unintended behavioran unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"pivot rectangular data to different "shapes"