How can you count how often each date appears in a data set?

827 Views Asked by At

I am working with a large health related database. Each event has a datetime tag on it. Example:

Admission DateTime
2016-12-20 03:04:05
2016-12-20 12:07:00
2016-12-20 13:11:15
2016-12-21 03:04:05
2016-12-21 03:04:05
2016-12-21 08:08:01
2016-12-22 05:05:05
2016-12-22 05:10:44
2016-12-23 03:04:25

What I would like from this data is to see how many times does a certain datetime appear. Specifically days. I know how to round down the datetime in R, but I am have trouble going from that step to the table below (which is my desired outcome):

Admission DateTime, Occurrences
2016-12-20, 3
2016-12-21, 3
2016-12-22, 2
2016-12-23, 1

Is there anyway I can do this in R without utilizing any packages (Installing packages needs permission from IT staff, and that can take a while to get).

2

There are 2 best solutions below

4
On BEST ANSWER

table should do the trick:

table(as.POSIXct(c("2016-12-20 03:04:05", "2016-12-20 12:07:00", "2016-12-20 12:07:00")))

You can wrap as.data.frame(...) around this expression to obtain a data frame.

3
On

Should you eventually use dplyr and the hallowed Tidyverse:

  library(dplyr)
  dataset%>%
  group_by(Datetime)%>%
  summarize(n=n())