-1

enter image description here

The dataframe above is the an example of the original one. I am trying to create following new dataframe based on this original one:

enter image description here

Thank you!

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81

1 Answers1

1

We can use xtabs from base R

xtabs(abundance ~ StationCode + SpeciesCode, df1)

-output

      SpeciesCode
StationCode AME BCF BKB CAP
       O-01   2   1   5   0
       O-02   1   0   1   1
       O-03   0   4   2   0
       O-04   0   0   8   1

data

df1 <- structure(list(SpeciesCode = c("AME", "AME", "BCF", "BCF", "CAP", 
"CAP", "BKB", "BKB", "BKB", "BKB"), StationCode = c("O-01", "O-02", 
"O-03", "O-01", "O-04", "O-02", "O-04", "O-01", "O-02", "O-03"
), abundance = c(2L, 1L, 4L, 1L, 1L, 1L, 8L, 5L, 1L, 2L)), 
class = "data.frame", row.names = c(NA, 
-10L))
akrun
  • 874,273
  • 37
  • 540
  • 662