I'm trying to transpose a wide dataset to a long tidy one. I use the tidyr::gather()
function alot for these kind of tasks, only now I have a pretty weird dataset.
The following is a small version of mine. As you can imagine that the columns with __1
behind them get repeated till number __16
or something in my real datframe. Is this possible to fix with tidyr
or dplyr
tools?
# A tibble: 1 x 10
code city party_short party_long votes seats party_short__1 party_long__1 votes__1 seats__1
<dbl> <chr> <chr> <chr> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
1 3630 Amsterdam PVDA Partij van de Arbeid 1833 5.00 HARLBEL Harlinger Belang 942 2.00
And for reproducability:
library(tidyverse)
df <- tibble(code = 3630,
city = "Amsterdam",
party_short = "PVDA",
party_long = "Partij van de Arbeid",
votes = 1833,
seats = 5,
party_short__1 = "HARLBEL",
party_long__1 = "Harlinger Belang",
votes__1 = 942,
seats__1 = 2)
With a desired output:
# A tibble: 2 x 6
code city party_short party_long votes seats
<dbl> <chr> <chr> <chr> <dbl> <dbl>
1 3630 Amsterdam PVDA Partij van de Arbeid 1833 5.00
2 3630 Amsterdam HARLBEL Harlinger Belang 942 2.00