2

I have a data frame with species, like this:

df <- data.frame("especie" = c("abies", "abies", "acacia", "acacia", "acacia"), "use"=c("ornamental", "wood", "wood", "medicine", "firewood"))

df
  especie        use
1   abies ornamental
2   abies       wood
3  acacia       wood
4  acacia   medicine
5  acacia   firewood

I want to "spread" it so it goes like:

df2 <- data.frame("species"=c("abies", "acacia"), "use1"=c("ornamental", "wood"), "use2"=c("wood", "medicine"),
                                      "use3"=c("", "firewood"), "use4"=c("", ""))

 df2
  species       use1     use2     use3 use4
1   abies ornamental     wood              
2  acacia       wood medicine firewood 

I don't want a column with the name of each level, so tidyr::spread does not do what i want; if the sepecies has only one "use", it should be in "use1", I've got no clue how can this be done

Elio Diaz
  • 566
  • 2
  • 19

1 Answers1

2

Here is one way of doing it, assuming that the row numbers are ordered in the same way you want to use "use1 ... use3", and use4 isn't important (its all blank?)

library(dplyr)
library(tidyr)

df %>%
  group_by(especie) %>%
  mutate(rowNum = paste0("use",row_number())) %>%
  spread(rowNum, use)

And the result is...

# A tibble: 2 x 4
# Groups:   especie [2]
  especie       use1     use2     use3
*  <fctr>     <fctr>   <fctr>   <fctr>
1   abies ornamental     wood       NA
2  acacia       wood medicine firewood
TBSRounder
  • 348
  • 1
  • 9