0

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).

Alokin
  • 461
  • 1
  • 4
  • 22
  • 1
    Your desired output is misleading. 2016-12-20 does not occur 3 times at 03:04:05 in the data set. Same with the next two rows. – Rich Scriven Dec 27 '16 at 21:09
  • 2
    Possible duplicate of [count number of rows in a data frame in R based on group](http://stackoverflow.com/questions/25293045/count-number-of-rows-in-a-data-frame-in-r-based-on-group) – nrussell Dec 27 '16 at 21:40

2 Answers2

2

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.

Thales
  • 585
  • 3
  • 9
  • Yes, I tried you method and with the data frame wrap I'm able to get a really nice looking data frame, the only issue is that the values are wrong (I did some simple SQL spot checking). – Alokin Dec 28 '16 at 14:45
  • For example, running the program i would get 200 entries for 12-20-2016, but the SQL search comes up with around 600. Could it be with how I am rounding the datetimes? I am using this: 'dates <- round(My_data, "days")' I'm trying to have the Data just reflect the days section of the datetime, and ignore the hours and minutes that come before it. I apologize for the messy content, I am new here. – Alokin Dec 28 '16 at 14:50
  • `table(as.Date(...)) ` works for the sample input in my answer; the problem with `round` may come from it returning a `POSIXlt` list. – Thales Dec 28 '16 at 20:33
  • Yeah, you're right. `round` caused a few unforeseen issues. I caved in and got lubridate and used the `floor_date` function instead. And then with your answer, I was able to get the correct answers. Thank you, I really appreciate the feedback/input. – Alokin Dec 29 '16 at 15:42
-1

Should you eventually use dplyr and the hallowed Tidyverse:

  library(dplyr)
  dataset%>%
  group_by(Datetime)%>%
  summarize(n=n())
Rhodo
  • 1,234
  • 4
  • 19
  • 35
  • 1
    I don't think you _need_ `dplyr` for this. Also, if you're going tidyverse you could simply use `count(dataset, Datetime)`. – Axeman Dec 27 '16 at 21:44
  • I got down voted for suggesting the tidyverse? Good Times – Rhodo Dec 28 '16 at 15:14
  • There is nothing wrong with using `dplyr`. Suggesting to a possibly new user that he _needs_ to use `dplyr` for a simple frequency table is perhaps a little misleading though. – Axeman Dec 28 '16 at 17:05