I have a yearmon
object:
我有一个共同的目标:
require(zoo)
date1 <- as.yearmon("Mar 2012", "%b %Y")
class(date1)
# [1] "yearmon"
How can I extract the month and year from this?
我如何从中提取月和年?
month1 <- fn(date1)
year1 <- fn(date1)
What function should I use in place of fn()
用什么函数代替fn()
6 个解决方案
#1
134
Use the format()
method for objects of class "yearmon"
. Here is your example date (properly created!)
对于类“yearmon”的对象使用format()方法。这是您的示例日期(正确创建!)
date1 <- as.yearmon("Mar 2012", "%b %Y")
Then we can extract the date parts as required:
然后根据需要提取日期部分:
> format(date1, "%b") ## Month, char, abbreviated
[1] "Mar"
> format(date1, "%Y") ## Year with century
[1] "2012"
> format(date1, "%m") ## numeric month
[1] "03"
These are returned as characters. Where appropriate, wrap in as.numeric()
if you want the year or numeric month as a numeric variable, e.g.
它们作为字符返回。如果需要将年份或数字月作为数值变量,请在适当的地方使用asn .numeric()包装。
> as.numeric(format(date1, "%m"))
[1] 3
> as.numeric(format(date1, "%Y"))
[1] 2012
See ?yearmon
and ?strftime
for details - the latter explains the placeholder characters you can use.
参见yearmon和?strftime获取详细信息——后者解释了您可以使用的占位符。
#2
97
The lubridate package is amazing for this kind of thing:
这类产品的润滑包装非常棒:
> require(lubridate)
> month(date1)
[1] 3
> year(date1)
[1] 2012
#3
15
I know the OP is using zoo
here, but I found this thread googling for a standard ts
solution for the same problem. So I thought I'd add a zoo
-free answer for ts
as well.
我知道OP在这里使用zoo,但是我发现这个线程在google上搜索同样的问题的标准ts解决方案。所以我想我应该为ts添加一个无动物的答案。
# create an example Date
date_1 <- as.Date("1990-01-01")
# extract year
as.numeric(format(date_1, "%Y"))
# extract month
as.numeric(format(date_1, "%m"))
#4
11
You can use format
:
您可以使用格式:
library(zoo)
x <- as.yearmon(Sys.time())
format(x,"%b")
[1] "Mar"
format(x,"%Y")
[1] "2012"
#5
5
For large vectors:
对于大型向量:
y = as.POSIXlt(date1)$year + 1900 # x$year : years since 1900
m = as.POSIXlt(date1)$mon + 1 # x$mon : 0–11
#6
0
The question did not state precisely what output is expected but assuming that for month you want the month number (January = 1) and for the year you want the numeric 4 digit year then assuming that we have just run the code in the question:
问题并没有明确说明预期的输出,但假设您想要一个月的月号(1月= 1),而想要数字4位数的年,那么假设我们刚刚运行了问题中的代码:
cycle(date1)
## [1] 3
as.integer(date1)
## [1] 2012
#1
134
Use the format()
method for objects of class "yearmon"
. Here is your example date (properly created!)
对于类“yearmon”的对象使用format()方法。这是您的示例日期(正确创建!)
date1 <- as.yearmon("Mar 2012", "%b %Y")
Then we can extract the date parts as required:
然后根据需要提取日期部分:
> format(date1, "%b") ## Month, char, abbreviated
[1] "Mar"
> format(date1, "%Y") ## Year with century
[1] "2012"
> format(date1, "%m") ## numeric month
[1] "03"
These are returned as characters. Where appropriate, wrap in as.numeric()
if you want the year or numeric month as a numeric variable, e.g.
它们作为字符返回。如果需要将年份或数字月作为数值变量,请在适当的地方使用asn .numeric()包装。
> as.numeric(format(date1, "%m"))
[1] 3
> as.numeric(format(date1, "%Y"))
[1] 2012
See ?yearmon
and ?strftime
for details - the latter explains the placeholder characters you can use.
参见yearmon和?strftime获取详细信息——后者解释了您可以使用的占位符。
#2
97
The lubridate package is amazing for this kind of thing:
这类产品的润滑包装非常棒:
> require(lubridate)
> month(date1)
[1] 3
> year(date1)
[1] 2012
#3
15
I know the OP is using zoo
here, but I found this thread googling for a standard ts
solution for the same problem. So I thought I'd add a zoo
-free answer for ts
as well.
我知道OP在这里使用zoo,但是我发现这个线程在google上搜索同样的问题的标准ts解决方案。所以我想我应该为ts添加一个无动物的答案。
# create an example Date
date_1 <- as.Date("1990-01-01")
# extract year
as.numeric(format(date_1, "%Y"))
# extract month
as.numeric(format(date_1, "%m"))
#4
11
You can use format
:
您可以使用格式:
library(zoo)
x <- as.yearmon(Sys.time())
format(x,"%b")
[1] "Mar"
format(x,"%Y")
[1] "2012"
#5
5
For large vectors:
对于大型向量:
y = as.POSIXlt(date1)$year + 1900 # x$year : years since 1900
m = as.POSIXlt(date1)$mon + 1 # x$mon : 0–11
#6
0
The question did not state precisely what output is expected but assuming that for month you want the month number (January = 1) and for the year you want the numeric 4 digit year then assuming that we have just run the code in the question:
问题并没有明确说明预期的输出,但假设您想要一个月的月号(1月= 1),而想要数字4位数的年,那么假设我们刚刚运行了问题中的代码:
cycle(date1)
## [1] 3
as.integer(date1)
## [1] 2012