1

Like to make a new data set where column 1 is distinct categorical variable and column 2 through to Column X, are the associated values of A. This is a one to many relationship.

When using spread got an error message given the 1 to many relationship

library(tidyr)
new_data <- spread(original_data, `State name`, `Park Location`)

Col 1 Col 2  Transform to  Col 1 Col 2 Col 3 Col 4 
A       C                    A     C    B     A
A       B                    B     A
A       A
B       A 

Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 687 rows:
camille
  • 16,432
  • 18
  • 38
  • 60
Ricky
  • 123
  • 9
  • Does this answer your question? [Problems with making my dataframe in 'wide' structure while trying using spread() function in R](https://stackoverflow.com/questions/56412910/problems-with-making-my-dataframe-in-wide-structure-while-trying-using-spread) – camille Nov 06 '19 at 20:21

1 Answers1

0

One option is to create a sequence column by 'Col1'

library(dplyr)
library(tidyr)
original_data %>%
   group_by(Col1) %>%
   mutate(rn = row_number()) %>%
   spread(rn, Col2)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    I can see the new tibble creates the horizontal data set I'm looking for. And creates new column names of 1 through 7 given the frequency of the 1 to many relationship in the data, I just needed to convert back into a data frame. TY – Ricky Nov 07 '19 at 14:46