[英]Separate day of year into month and day of month columns in R
For simplicity, I have data that has two columns.为简单起见,我的数据有两列。 One column is the year (year) and the other is the number of days (yday).
一列是年份(year),另一列是天数(yday)。 So year with a value of 1980 and yday with a value of 1 is January 1, 1980. Year with a value of 1980 and yday with a value of 365 is December 31, 1980. How do I separate the single yday column into two columns;
因此,值为 1980 的年份和值为 1 的 yday 为 1980 年 1 月 1 日。值为 1980 的年份和值为 365 的 yday 为 1980 年 12 月 31 日。如何将单个 yday 列分成两列; a month column and the day of the month column?
月份列和月份列的日期? For example, 365 would be 12 for the month and 31 for the day.
例如,365 表示月份为 12,日为 31。 Thanks in advance.
提前致谢。
Create a Date from the yday
+ year
columns, then extract the day of month, and month separately:从
yday
+ year
列创建一个 Date,然后分别提取月中的日期和月份:
dat <- data.frame(year=1980, yday=c(1,365))
# year yday
#1 1980 1
#2 1980 365
dat[c("month","day")] <- lapply(c("%m","%d"), \(x) {
d <- as.Date(paste(dat$year, dat$yday), format="%Y %j")
as.integer(format(d, x))
})
# year yday month day
#1 1980 1 1 1
#2 1980 365 12 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.