简体   繁体   English

将日期拆分为年、月和日的不同列

[英]Split date into different columns for year, month and day

I have zoo objects that look like:我有看起来像的动物园对象:

head(obs)头(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 .我想将索引分成 3 列(单独列中的年、月和日),以便我可以使用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.其中 CET 是原始文件,日期在第 1 列,pp 在第 2 列。

Thanks for helping!感谢您的帮助!

1) columns . 1) We can use lubridate's year / month / day or chron's month.day.year :我们可以使用 lubridate 的year / month / day或 chron 的month.day.year

1a) columns via lubridate 1a)通过 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 1b)通过 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 . 2)聚合 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:我们可以直接将aggregate.zoo与原始 zoo object, z一起使用,使用 lubridate 或 chron 或仅使用 zoo 中的yearmon ,具体取决于您想要做什么:

2a) aggregate using lubridate 2a)使用 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 2b)使用 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 2c)使用 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 :如果我们希望总结每年/每月,而不是将所有一月份的月份、所有二月份的月份放在一起等等,那么我们既不需要 chron 也不需要 lubridate 而是可以使用 zoo 的yearmon

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

aggregate(z, yearmon, mean)
dtstr <- as.character( index(CET))

CET$yr <- sapply(strsplit(dtstr, "-") , "[", 1)
CET$mon <- sapply(strsplit(dtstr, "-") , "[", 2)
CET$dt <- sapply(strsplit(dtstr, "-") , "[", 3)

You can try:你可以试试:

CET$year <- format(CET[,1], "%Y") # year
CET$month <- format(CET[,1], "%m") # month
CET$day <- format(CET[,1], "%d") # day
require(lubridate)
maindata1 <- cbind(maindata1, day=day(maindata1$Date), month=month(maindata1$date), year=year(maindata1$date))

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

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