使用zoo组合两列以在R中形成Date

时间:2017-07-10 13:02:08

标签: r zoo

我正在尝试使用zoo在data.table中使用两列来表示Date。

数据$ Date< - as.yearmon(粘贴(数据$月,数据$年),"%Y%m") 但我在NA的所有内容

以下是数据的样子:

Year    Month   State   County  Rate
2015    October California  Santa Clara County  4.0
2015    March   California  Santa Clara County  4.4
2015    August  California  Santa Clara County  4.1
2015    May California  Santa Clara County  4.1
2015    January California  Santa Clara County  4.7

1 个答案:

答案 0 :(得分:3)

你有两个问题。一,您正在粘贴MonthYear,但告诉您发送YearMonth。此外,%m表示月份为小数1-12。您希望%B获取月份的全名。您需要切换粘贴的顺序并更改格式。

data$Date <- as.yearmon(paste(data$Year,data$Month), "%Y %B")

   Year   Month      State             County Rate     Date
1: 2015 October California Santa Clara County  4.0 Oct 2015
2: 2015   March California Santa Clara County  4.4 Mar 2015
3: 2015  August California Santa Clara County  4.1 Aug 2015
4: 2015     May California Santa Clara County  4.1 May 2015
5: 2015 January California Santa Clara County  4.7 Jan 2015