Formatting R dates while saving them as dates

I feel that there is a pretty easy way to do this, but I don't find it easy ...

I work with R to extract data from a data set, and they summarize it over several different characteristics. One of them is the month in which the event is planned / occurred. We have the exact date of the event in the database, something like this:

person_id date_visit 1 2012-05-03 2 2012-08-13 3 2012-12-12 ... 

I would like to use the table() function to create a pivot table that would look something like this:

 Month Freq Jan 12 1 Feb 12 2 Mar 12 1 Apr 12 3 ... 

My problem is this. I read the data and used as.Date() to convert character strings to dates. I can use format.Date() to get dates formatted as January 12th, March 12th, etc. But when you use format.Date() , you will get character strings again. This means that when you apply table() to them, they come out in alphabetical order (my current set is August 12, July 12, June 12, March 12, etc.).

I know that in SAS you can use the format to change the appearance of the date, saving it as a date (so you can use date operators on it anyway). Is it possible to do the same using R?

My plan is to create a good data frame in a few steps, and then (after making sure that all dates have been converted to strings, for compatibility reasons) use xtable() to make a nice LaTeX output.

Here is my code for now.

 load("temp.RData") ds$date_visit <- as.Date(ds$date_visit,format="%Y-%m-%d") table(format.Date(safebeat_recruiting$date_baseline,format="%b %Y")) 

ETA: I would rather just do it in Base R if I can, but if I need to, I can always use the extra package.

+4
source share
2 answers

month.abb is a constant vector in R and can be used to sort by the first three letters of the names row for the table.

 ds <- data.frame(person_id=1:3, date_visit=as.Date(c("2012-05-03", "2012-08-13", "2012-12-12"))) table(format( ds$date_visit, format="%b %Y")) tbl <- table(format( ds$date_visit, format="%b %Y")) tbl[order( match(substr(names(tbl), 1,3), month.abb) )] May 2012 Aug 2012 Dec 2012 1 1 1 

With extra years, you will see all May so that it is necessary:

  tbl[order( substr(names(tbl), 5,8), match(substr(names(tbl), 1,3), month.abb) )] 
+1
source

You can use yearmon class from zoo package

 require("zoo") ds <- data.frame(person_id=1:3, date_visit=c("2012-05-03", "2012-08-13", "2012-12-12")) ds$date_visit <- as.yearmon(ds$date_visit) ds person_id date_visit 1 1 May 2012 2 2 Aug 2012 3 3 Dec 2012 
+4
source

All Articles