简体   繁体   English

在保留课程日期的同时将日期格式化为月-年

[英]Format dates to Month-Year while keeping class Date

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.我正在使用 R 从数据集中提取数据,然后他们通过许多不同的特征对其进行总结。 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:我想使用table()函数来生成一个如下所示的汇总表:

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.我已读取数据并使用as.Date()将字符串转换为日期。 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.我可以使用format.Date()来获取格式化为 Jan 12、Mar 12 等的日期。但是当你使用format.Date() ,你又以字符串结束。 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).这意味着当您对它们应用table()时,它们会按字母顺序出现(我当前的集合是 8 月 12 日、7 月 12 日、6 月 12 日、3 月 12 日等等)。

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).我知道在 SAS 中,您可以使用格式来更改日期的外观,同时将其保留为日期(因此您仍然可以对其进行日期运算符)。 Can the same thing be done using R?使用 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.我的计划是通过多个步骤构建一个漂亮的数据框,然后(在确保所有日期都转换为字符串之后,出于兼容性原因)使用xtable()制作一个漂亮的 LaTeX 输出。

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. ETA:如果可以的话,我更愿意只在 Base R 中进行,但如果必须的话,我总是可以使用额外的包。

You could use the yearmon class from the zoo package您可以使用zoo包中的yearmon

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

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. month.abb是 R 中的常量向量,可用于对表格names字符串的前三个字母进行排序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM