Tdebeus
Tdebeus

Reputation: 1599

Spread two rows to one with tidyr

I've seen this, this and this but still don't know how to solve the following problem within the tidyr::spread() function.

Here's my example data frame:

libary(tidyverse)

df <- structure(list(Jaar = c(2014L, 2018L), Gemeente = c("Stichtse Vecht", 
"Stichtse Vecht"), GMcode = c("GM1904", "GM1904"), Partij = c("VVD", 
"VVD"), Aantal_stemmen = c(4347L, 0L)), .Names = c("Jaar", "Gemeente", 
"GMcode", "Partij", "Aantal_stemmen"), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

result:

# A tibble: 2 x 5
   Jaar Gemeente       GMcode Partij Aantal_stemmen
  <int> <chr>          <chr>  <chr>           <int>
1  2014 Stichtse Vecht GM1904 VVD              4347
2  2018 Stichtse Vecht GM1904 VVD                 0

When I run the following code I don't get the desired one row but two with NA's:

df %>%
  rowid_to_column() %>% # Without this in my original dataframe I'll get an error: Error: Duplicate identifiers for rows 
  spread(Jaar, Aantal_stemmen)

result:

# A tibble: 5,938 x 6
   rowid Gemeente       GMcode Partij   `2014` `2018`
   <int> <chr>          <chr>  <chr>     <int>  <int>
 1     1 Stichtse Vecht GM1904 VVD        4347     NA
 2     2 Stichtse Vecht GM1904 VVD          NA      0

Upvotes: 2

Views: 476

Answers (1)

KoenV
KoenV

Reputation: 4283

I am not exactly sure what you want as you didn't provide the wanted output. I hope the following is of help to you.

The call to rowid_to_column generates a column with 2 rows. That is what it is intended to do. Dropping it solves your problem:

df %>%
  # rowid_to_column() %>%
  spread(Jaar, Aantal_stemmen)

which gives

# A tibble: 1 x 5
  Gemeente       GMcode Partij `2014` `2018`
  <chr>          <chr>  <chr>   <int>  <int>
1 Stichtse Vecht GM1904 VVD      4347      0

Please let me know whether this is what you want.

Upvotes: 4

Related Questions