将日期格式设置为“月”,同时保持“日期”类 [英] Format dates to Month-Year while keeping class Date

查看:107
本文介绍了将日期格式设置为“月”,同时保持“日期”类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得有一个很简单的方法可以做到这一点,但是我发现它并不容易...

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

我正在与R一起从中提取数据数据集,并通过许多不同的特征对其进行汇总。其中之一是预定/已发生事件的月份。我们在数据库中有事件的确切日期,如下所示:

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

我想使用 table()函数生成一个汇总表,看起来像这样:

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

我的问题是这个。我已读取数据,并使用 as.Date()将字符串转换为日期。我可以使用 format.Date()来获取日期格式为1月12日,3月12日等。但是当您使用 format.Date()时, ,您将再次得到字符串。这意味着当您对它们应用 table()时,它们会按字母顺序显示(我当前的设置是8月12日,7月12日,6月12日,3月12日,依此类推)。

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

我知道在SAS中,您可以使用一种格式来更改日期的外观,同时将其保存为日期(因此您仍然可以使用日期运算符在上面)。

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?

我的计划是通过多个步骤构建一个不错的数据框,然后(确保所有日期都在出于兼容性原因而转换为字符串)使用 xtable()进行LaTeX输出。

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.

这是我的代码

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:如果可以的话,我宁愿只在Base R中这样做,但如果需要的话,我总是可以使用其他软件包。 / p>

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.

推荐答案

month.abb 是R中的常量向量,可以使用对表的 names 字符串的前三个字母进行排序。

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

这篇关于将日期格式设置为“月”,同时保持“日期”类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆