0

I have the following data frame:

 Date          Number      Species     Group
2019-01-01      3             XX        Insect
2019-01-01      10            XY        Bird
2019-01-02      24            XZ        Plant
2019-01-02      1             XX        Insect
2019-01-02      26            XX        Insect
2019-01-03      40            XY        Bird
2019-01-03       5            XY        Bird

The Variable Date is in POSIXct format, Number is an integer and Species and Group are character variables.

What I want to know is, how many different Species per Date are being reported. So as a result I need something like this:

  Date           Species_Number       
 2019-01-01            2   
 2019-01-02            2
 2019-01-03            1

I tried a few things with the count and aggregate function, but nothing really worked. I need to do the same thing with the Group variable later, but I guess that will work the same way...thanks for your advice!

HelmiYeet
  • 17
  • 5

1 Answers1

0
library( data.table )

DT[, .(Species_Number = uniqueN(Species)), by = Date]
#          Date Species_Number
# 1: 2019-01-01              2
# 2: 2019-01-02              2
# 3: 2019-01-03              1

#sample data
DT <- fread("Date          Number      Species     Group
2019-01-01      3             XX        Insect
2019-01-01      10            XY        Bird
2019-01-02      24            XZ        Plant
2019-01-02      1             XX        Insect
2019-01-02      26            XX        Insect
2019-01-03      40            XY        Bird
2019-01-03       5            XY        Bird")
Wimpel
  • 26,031
  • 1
  • 20
  • 37