0

I'm struggling to understand how to go from a long to wide data format using tidyr's spread function. Say I have the following data set:

 tst <- data.frame(year = c(2000, 2001, 2002, 2000, 2001, 2002), 
                   site = c("S1", "S1", "S1", "S2", "S2", "S2"), 
                   numbers.one = runif(6), numbers.two = runif(6)) 

which looks like,

  year site numbers.one numbers.two
1 2000   S1   0.8473614  0.40118748
2 2001   S1   0.6581544  0.14398803
3 2002   S1   0.4567127  0.59807617
4 2000   S2   0.4626209  0.02423530
5 2001   S2   0.7317331  0.97254451
6 2002   S2   0.7858409  0.01235655

I would like to collapse this into the following format:

  year S1.numbers.one S1.numbers.two S2.numbers.one S2.numbers.two
1 2000      0.8473614     0.40118748      0.4626209     0.02423530
2 2001      0.6581544     0.14398803      0.7317331     0.97254451
3 2002      0.4567127     0.59807617      0.7858409     0.01235655

I'm sure this is very simple if I understood the syntax. spread(site, numbers.one) produces S1 and S2 columns for numbers.one values, but not numbers.two (plus multiple rows for each year and NAs as fillers) and I don't see how to specify multiple columns to spread. Am I missing a step, such as gathering all the same years together first and then spreading?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Dan
  • 11,370
  • 4
  • 43
  • 68

0 Answers0