2

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
Tdebeus
  • 1,519
  • 5
  • 21
  • 43

1 Answers1

4

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.

KoenV
  • 4,113
  • 2
  • 23
  • 38
  • Ah yes but without it my original dataset produces this error: `Error: Duplicate identifiers for rows` – Tdebeus Mar 21 '18 at 14:58
  • @Tdebeus Then please provide a dataset which exhibits this unwanted behavior. I worked with the dataset you gave ..... – KoenV Mar 21 '18 at 15:03
  • You're right, Thanks for your answer! I'll just delete the duplicate data. think that's a better approach. Thanks though! – Tdebeus Mar 21 '18 at 15:08
  • My pleasure. Glad I could help. – KoenV Mar 21 '18 at 15:09