4

I am learning tidyr and doing a small exercise to transform iris data set from wide to long.

The original data set:

   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1  setosa          5.1         3.5          1.4         0.2
2  setosa          4.9         3.0          1.4         0.2
3  setosa          4.7         3.2          1.3         0.2
4  setosa          4.6         3.1          1.5         0.2
5  setosa          5.0         3.6          1.4         0.2
6  setosa          5.4         3.9          1.7         0.4

The resulting data set I want:

  Species  Part Length Width
1  setosa Petal    1.4   0.2
2  setosa Petal    1.4   0.2
3  setosa Petal    1.3   0.2
4  setosa Petal    1.5   0.2
5  setosa Petal    1.4   0.2
6  setosa Petal    1.7   0.4

The code I wrote for manipulating data set:

iris_re <- iris[,c(5,1,2,3,4)]

iris.wide <- iris_re %>% 
  gather(key = "flower_att", value = "measurement",
         -Species) %>%
  separate(flower_att, into = c("Part","Method")) %>%
  spread(Method,measurement)

But the final line of spread() gives me an error:

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

I did not expect this happen and I am still struggling with it. Thank you!

2
  • Without any identifiers, your script won't know how to put rows together. If you search the error message, you should see a lot of SO posts getting around this (the newer pivot_wider alternative helps), including this one on the same dataset
    – camille
    Feb 5, 2020 at 20:29
  • @camille OK. I will read the link and try some small examples. Thanks!
    – Steve
    Feb 5, 2020 at 20:40

1 Answer 1

4

We can use pivot_longer from tidyr, which can also take multiple columns

library(dplyr)
library(tidyr)
iris_re %>%
  pivot_longer(cols = -Species, names_to = c("Part", ".value"), names_sep= "[.]") %>%
  head
#  Species  Part Length Width
#1  setosa Sepal    5.1   3.5
#2  setosa Petal    1.4   0.2
#3  setosa Sepal    4.9   3.0
#4  setosa Petal    1.4   0.2
#5  setosa Sepal    4.7   3.2
#6  setosa Petal    1.3   0.2

The error in spread can occur when there are more than one unique combinations exist. With pivot_wider, it is now replaced with a warning and would return a list column if there are duplicates and then we can unnest. Or another way is to create a sequence column grouped by the column identifier that have duplicates to make a unique row identifier i.e.

iris_re %>% 
  gather(key = "flower_att", value = "measurement",
         -Species) %>%
  separate(flower_att, into = c("Part","Method")) %>%
  group_by(Species, Part, Method) %>%
  mutate(rn = row_number()) %>% 
  ungroup %>%
  spread(Method,measurement)
4
  • Could you please explain more on "more than one unique combinations" by using iris data set? Does this mean the combination values from Species, Part and Method columns(the resulting table after separate()function)? I need to read docs and see examples about the new functions pivot_wider/longer . The syntax is different from old function gather/spread.
    – Steve
    Feb 5, 2020 at 20:34
  • 1
    @Steve After the separate step, if you check the count(Species, Part, Method) it will be more than 1 and spread needs a count of 1, otherwise, it won't know which value belongs to which row
    – akrun
    Feb 5, 2020 at 20:35
  • @Steve Regarding pivot_wider/longer, it is more a generalized version of gather/spread. Here, the pivot_longer doesn't need to do two reshape i.e. convertiing to 'long' and then to 'wide'. It does in a single step
    – akrun
    Feb 5, 2020 at 20:36
  • 1
    Yes. The new function seems easier and more compatible. Thanks!
    – Steve
    Feb 5, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.