-2

I have a data.frame like this:

# Scientific.Name             Year

# Sympetrum                   2012
# Celithemis eponina          2012
# Celithemis elisa            2012
# Celithemis elisa            2012
# Cordulegaster maculata      2011
# Calopteryx maculata         2011
# Celithemis elisa            2013
# Celithemis elisa            2013

I want to make a data.frame that looks like this:

                                Year
# Scientific.Name               2011     2012     2013
# Sympetrum                         0        1        0
# Celithemis eponina                0        1        0
# Celithemis elisa                  0        2        2
# Cordulegaster maculata            1        0        0
# Calopteryx maculata               1        0        0

I am hoping to have one column for unique species names, the rest of the columns to be unique years. I want to count how many times a unique species has been collected in each year and have it be returned as a data.frame. The actual data.frame I want to transform is almost 3,000 observations but I only need 82 rows (82 species).

jek185
  • 1
  • 1

1 Answers1

2

The table() function does the trick

df <- data.frame(Scientific.Name = c("Sympetrum", "Celithemis eponina", "Celithemis elisa", "Celithemis elisa",
                                     "Cordulegaster maculata", "Calopteryx maculata", "Celithemis elisa", "Celithemis elisa"),
                 Year = c(2012, 2012, 2012, 2012, 2011, 2011, 2013, 2013))
table(df)


                        Year
Scientific.Name          2011 2012 2013
  Calopteryx maculata       1    0    0
  Celithemis elisa          0    2    2
  Celithemis eponina        0    1    0
  Cordulegaster maculata    1    0    0
  Sympetrum                 0    1    0
astrofunkswag
  • 2,608
  • 12
  • 25