spread error: Each row of output must be identified by a unique combination of keys.

Hello all,

I have a data frame (df) that looks like this:

   month year             label         color    value
1      1 2012      0. Not labelled     #ffffff     1
2      1 2012      0. Not labelled     #ffffff     1
3      2 2012      0. Wet              #ffffff     1
4      3 2012      0. Wet              #ffffff     1
5      4 2012      0. average          #ffffff     1
6      5 2012      0. average          #ffffff     1
...
94     7 2015      4. Dry              #d7191c     1
95     6 2014      4. Dry              #d7191c     1
96     8 2018      4. Dry              #d7191c     1

With the following line I'm trying to spread my dataframe by the labels:

df <- spread(df, label, color)

But I get the following Error:

Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 2 rows:
* 1, 2

I dont understand the Error, and thus have no idea on how to fix it.
Could anyone explain me the Error and explain me how to fix it?
Thanks in advance

The error message tells you the problem. Rows 1 and 2 are the same, so spread() cannot work.

Check whether one of the rows should be deleted or else whether the data is correct.

You could use pivot_wider(), but I doubt you want list columns as output:

df <- pivot_wider(df, names_from = label, values_from = color)

Thanks a lot, removing the first row solved the problem

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.