英文:
How to convert a numeric variable that counts the number of months since a certain point into a variable with an interpretable date (R)?
问题
我有R中的数据,其中有一个数字变量,表示自1960年1月以来的月数。
让我们说:
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
我如何创建一个新的日期变量,以有价值的方式显示这些日期?
我希望得到类似这样的结果:
date <- (1960-01,1960-02,1961-01,1961-02,1962-01,1962-02)
我相信这是一个简单的修复,但我已经尝试过了,似乎无法弄清楚...
英文:
I have data in R that has a numeric variable that counts the number of months since Jan. 1960.
Let’s say:
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
How can I create a new date variable that will show me these dates in a valuable way?
I'm hoping for something like:
date <- (1960-01,1960-02,1961-01,1961-02,1962-01,1962-02)
I'm sure it's an easy fix but I've tried identifying this and I can't seem to figure it out...
答案1
得分: 2
这是什么样子的吗?
library(lubridate)
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
date_ref = as.Date("1960-01-01")
date_ref %m+% months(months_since_1960)
英文:
Is it something like that ?
library(lubridate)
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
date_ref = as.Date("1960-01-01")
date_ref %m+% months(months_since_1960)
答案2
得分: 0
- 创建一个yearmon变量,它内部表示日期,由年份+分数表示,其中分数为0、1/12、…、11/12,代表12个月。然后我们可以使用它或将其转换为Date,从而有多种可能性。
library(zoo)
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
ym <- as.yearmon(1960) + months_since_1960 / 12
ym # 年份和月份
## [1] "Jan 1960" "Feb 1960" "Jan 1961" "Feb 1961" "Jan 1962" "Feb 1962"
as.Date(ym) # 月初
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
as.Date(ym, frac = 1) # 月末
## [1] "1960-01-31" "1960-02-29" "1961-01-31" "1961-02-28" "1962-01-31" "1962-02-28"
- 使用基本的R(没有包)可以这样获得每月的第一天:
dat <- months_since_1960 |
lapply(\(x) tail(seq(as.Date("1960-01-01"), length = x+1, by = "month"), 1)) |
do.call(what = "c")
dat
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
或者获取每月的最后一天:
```R
as.Date(cut(dat + 31, "month")) - 1
## [1] "1960-01-31" "1960-02-29" "1961-01-31" "1961-02-28" "1962-01-31" "1962-02-28"
2a) 或者使用相同的转换来获取月末日期(如果需要的话):
dat2 <- as.Date(ISOdate(1960 + (months_since_1960 %/% 12), months_since_1960 %% 12 + 1, 1))
dat2
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
英文:
1) yearmon First create a yearmon variable which internally represents a date by year + fraction where fraction is 0, 1/12, ..., 11/12 for the 12 months. Then we can use that or convert to Date giving several possibilities.
library(zoo)
months_since_1960 <- c(0, 1, 12, 13, 24, 25)
ym <- as.yearmon(1960) + months_since_1960 / 12
ym # year and month
## [1] "Jan 1960" "Feb 1960" "Jan 1961" "Feb 1961" "Jan 1962" "Feb 1962"
as.Date(ym) # first of month
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
as.Date(ym, frac = 1) # last of month
## [1] "1960-01-31" "1960-02-29" "1961-01-31" "1961-02-28" "1962-01-31" "1962-02-28"
2) Base R With base R (no packages) we can do this to get the first of the month:
dat <- months_since_1960 |>
lapply(\(x) tail(seq(as.Date("1960-01-01"), length = x+1, by = "month"), 1)) |>
do.call(what = "c")
dat
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
or for last of of month
as.Date(cut(dat + 31, "month")) - 1
## [1] "1960-01-31" "1960-02-29" "1961-01-31" "1961-02-28" "1962-01-31" "1962-02-28"
2a) or this where we can use the same transformation to get end of month if desired:
dat2 <- as.Date(ISOdate(1960 + (months_since_1960 %/% 12), months_since_1960 %% 12 + 1, 1))
dat2
## [1] "1960-01-01" "1960-02-01" "1961-01-01" "1961-02-01" "1962-01-01" "1962-02-01"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论