The dataframe above is the an example of the original one. I am trying to create following new dataframe based on this original one:
Thank you!
The dataframe above is the an example of the original one. I am trying to create following new dataframe based on this original one:
Thank you!
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
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))