0

I am trying to take data that looks like this:

Precinct Office    Votes
1        auditor   10
1        president 9
2        auditor   6
2        president 8

to have 3 columns, precinct, auditor and president, with the values being what is now Votes. I have tried using dplyr::spread(), but end up with something like this:

Precinct auditor president
1        10      NA
1        NA      9
2        6       NA
2        NA      8

How do I get rid of the staggered NAs so that I have half as many rows, like this:

Precinct auditor president
1        10      9
2        6       8
Emma
  • 63
  • 1
  • 6
  • Just do `df1 %>% group_by(Precinct) %>% summarise_all(min, na.rm = TRUE)` or `df1 %>% group_by(Precinct) %>% summarise_all(na.omit)` – akrun Jun 15 '18 at 20:56
  • Based on the input data showed, `spread(df1, Office, Votes)` gives me the expected output rather than having some NA values – akrun Jun 15 '18 at 21:02
  • I can't reproduce your result with NA's. `tidyr::spread(dat, Office, Votes)` works for me on your dataset to get the desired result. Can you add the code you used? – aosmith Jun 15 '18 at 21:03
  • [How to reshape data from long to wide format?](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) – Henrik Jun 15 '18 at 21:15
  • `spread(df, Office, Votes)` is what I was using, but pairing that with `df %>% group_by(Precinct) %>% summarise_all(min, na.rm = TRUE)` from @akrun worked. – Emma Jun 15 '18 at 21:25
  • Are there any other variables in your real dataset? Is your dataset grouped? I'm using tidyr 0.8.1 and your code works fine for me. It seems like some information is missing. If you want to get to the bottom of it try adding a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to your question, including the output of `dput(head(df))` and your code. – aosmith Jun 16 '18 at 02:56

1 Answers1

0

You can use complete.cases

df[complete.cases(df), ]
Zac R.
  • 538
  • 3
  • 17