Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:35:25+00:00 2026-06-06T05:35:25+00:00

I feel like there’s a pretty simple way to do this, but I’m not

  • 0

I feel like there’s a pretty simple way to do this, but I’m not finding it easily…

I am working with R to extract data from a dataset and them summarize it by a number of different characteristics. One of them is the month in which an event is scheduled / has 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 generate a summary table that would look something like this:

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

My issue is this. I’ve read the data in and used as.Date() to convert character strings to dates. I can use format.Date() to get the dates formatted as Jan 12, Mar 12, etc. But when you use format.Date(), you end up with character strings again. This means when you apply table() to them, they come out in alphabetical order (my current set is Aug 12, Jul 12, Jun 12, Mar 12, and so forth).

I know that in SAS, you could use a format to change the appearance of a date, while preserving it as a date (so you could still do date operators on it). Can the same thing be done using R?

My plan is to build a nice data frame through a number of steps, and then (after making sure that all the dates are converted to strings, for compatibility reasons) use xtable() to make a nice LaTeX output.

Here’s my code at present.

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’d prefer to just do it in Base R if I can, but if I have to I can always use an additional package.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:35:29+00:00Added an answer on June 6, 2026 at 5:35 am

    month.abb is a constant vector in R and can be used to sort on the first three letter of the string of names 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 additional years you would see the “May”s all together so this would be needed:

     tbl[order( substr(names(tbl), 5,8),  match(substr(names(tbl), 1,3), month.abb) )]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like there's a simple solution to this, but I'm not really sure
I feel like there should be a simple way to do this but I
I feel like there should be a simple way to do this, but I
I have this mostly working, but I feel like there has to be a
Sorry for the simple question but I feel like there's a smarter way to
OK, this is working but I feel there is a better way to do
I feel like there must be something really simple that I'm not seeing here.
I feel like there is an obvious answer to this, but it's been eluding
I feel like there is some way of doing this with a shortcut: $date_array
I've seen this question but feel like there has to be a cleaner jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.