将年份,月份和日期分成不同的列 [英] Split date into different columns for year, month and day

查看:136
本文介绍了将年份,月份和日期分成不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动物园对象,如下所示:



head(obs)

 索引pp 
1932-01-01 0
1932-01-02 0.2
1932-01-03 0

,我想将索引分为3列(分隔列中的年份,月份和日期),因此我可以使用 ddply



我不知道是否有任何差异,但我的日期是使用以下方式创建的:

 日期<  -  as.Date(CET [,1],%d-%m-%Y)
obs< xts(CET [,2],日期)

其中CET是原始文件,日期在第1列和/ p>

感谢您的帮助!

解决方案

1)。我们可以使用lubridate的 / month / day 或chron's month.day.year



1a)列通过lubridate

  library(zoo)
z< - zoo(1:1000,as.Date(1932-01-01)+ 0:999)

库(lubridate)
tt < - 时间(z)
zz < - cbind(z,year = year(tt),month = month (tt),day = day(tt))

1b) strong>

  library(zoo)
z< - zoo(1:1000,as.Date(1932- 01-01)+ 0:999)

库(chron)
zz< - with(month.day.year(time(z)),zoo(cbind日,月,年)))

2)聚合。但是,我们并不需要首先创建列。我们可以直接使用原始动物园对象 z ,使用lubridate或chron或只是使用 aggregate.zoo 来自动物园的code> yearmon 取决于你想要做什么:



2a)聚合使用lubridate

  library(zoo)
z< - zoo(1:1000,as.Date(1932 -01-01)+ 0:999)

库(lubridate)
聚合(z,日,平均)
聚合(z,月,平均)
聚合(z,年,平均)

2b)使用chron

  library(zoo)
z< - zoo(1:1000,as.Date(1932-01- 01)+ 0:999)

库(chron)
mdy< - month.day.year(time(z))

aggregate ,mdy $ day,mean)
aggregate(z,mdy $ month,mean)
aggregate(z,mdy $ year,mean)

#或
ct < - as.chron(time(z))

aggregate(z,days(ct),mean)
aggregate(z,months(ct),mean)
聚合(z,年(ct),平均值)

#天(ct)和年(ct)实际上可以将
#缩短为上述上下文中的几天和几年
#(除了它们将会工作几个月
聚合(z,天,平均值)
聚合(z,年,平均值)

2c)使用年份汇总



如果我们希望每年/一起,所有二月份在一起等等,那么我们不需要时间也不需要浪费,而是可以使用动物园的 yearmon

  library(zoo)
z< - zoo(1:1000,as.Date(1932-01-01)+ 0:999)

aggregate(z,yearmon,mean)


I have zoo objects that look like:

head(obs)

      Index pp
1932-01-01  0
1932-01-02  0.2
1932-01-03  0

and I want to split the index into 3 columns (years, months and days in separate columns) so I can do some analyses per day of month using ddply.

I don't know if it makes any difference but my dates were created using:

dates <- as.Date(CET[,1], "%d-%m-%Y")
obs <- xts(CET[,2], dates)

where CET is the original file with dates in column 1 and pp in column 2.

Thanks for helping!

解决方案

1) columns. We can use lubridate's year/month/day or chron's month.day.year:

1a) columns via lubridate

library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)

library(lubridate)
tt <- time(z)
zz <- cbind(z, year = year(tt), month = month(tt), day = day(tt))

1b) columns via chron

library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)

library(chron)
zz <- with(month.day.year(time(z)), zoo(cbind(z, day, month, year)))

2) aggregate. However, we do not really need to create columns in the first place. We can just use aggregate.zoo directly with the original zoo object, z, using lubridate or chron or just using yearmon from zoo depending on what it is that you want to do:

2a) aggregate using lubridate

library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)

library(lubridate)
aggregate(z, day, mean)
aggregate(z, month, mean)
aggregate(z, year, mean)

2b) aggregate using chron

library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)

library(chron)
mdy <- month.day.year(time(z))

aggregate(z, mdy$day, mean)
aggregate(z, mdy$month, mean)
aggregate(z, mdy$year, mean)

# or
ct <- as.chron(time(z))

aggregate(z, days(ct), mean)
aggregate(z, months(ct), mean)
aggregate(z, years(ct), mean)

# days(ct) and years(ct) can actually
# be shortened to just days and years within the above context
# (and that would work for months too except they would be out of order)
aggregate(z, days, mean)
aggregate(z, years, mean)

2c) aggregate using yearmon

If we wish to summarize each year/month rather than lumping all January months together, all February months together, etc. then we need neither chron nor lubridate but rather can use zoo's yearmon:

library(zoo)
z <- zoo(1:1000, as.Date("1932-01-01") + 0:999)

aggregate(z, yearmon, mean)

这篇关于将年份,月份和日期分成不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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