How to convert a numeric variable that counts the number of months since a certain point into a variable with an interpretable date (R)?

huangapple go评论66阅读模式
英文:

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 &lt;- 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 &lt;- (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 &lt;- c(0, 1, 12, 13, 24, 25)
date_ref = as.Date(&quot;1960-01-01&quot;)
date_ref %m+% months(months_since_1960)

答案2

得分: 0

  1. 创建一个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"
  1. 使用基本的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 &lt;- c(0, 1, 12, 13, 24, 25)

ym &lt;- as.yearmon(1960) + months_since_1960 / 12

ym # year and month
## [1] &quot;Jan 1960&quot; &quot;Feb 1960&quot; &quot;Jan 1961&quot; &quot;Feb 1961&quot; &quot;Jan 1962&quot; &quot;Feb 1962&quot;

as.Date(ym) # first of month
## [1] &quot;1960-01-01&quot; &quot;1960-02-01&quot; &quot;1961-01-01&quot; &quot;1961-02-01&quot; &quot;1962-01-01&quot; &quot;1962-02-01&quot;

as.Date(ym, frac = 1) # last of month
## [1] &quot;1960-01-31&quot; &quot;1960-02-29&quot; &quot;1961-01-31&quot; &quot;1961-02-28&quot; &quot;1962-01-31&quot; &quot;1962-02-28&quot;

2) Base R With base R (no packages) we can do this to get the first of the month:

dat &lt;- months_since_1960 |&gt;
  lapply(\(x) tail(seq(as.Date(&quot;1960-01-01&quot;), length = x+1, by = &quot;month&quot;), 1)) |&gt;
  do.call(what = &quot;c&quot;)
dat
## [1] &quot;1960-01-01&quot; &quot;1960-02-01&quot; &quot;1961-01-01&quot; &quot;1961-02-01&quot; &quot;1962-01-01&quot; &quot;1962-02-01&quot;

or for last of of month

as.Date(cut(dat + 31, &quot;month&quot;)) - 1
## [1] &quot;1960-01-31&quot; &quot;1960-02-29&quot; &quot;1961-01-31&quot; &quot;1961-02-28&quot; &quot;1962-01-31&quot; &quot;1962-02-28&quot;

2a) or this where we can use the same transformation to get end of month if desired:

dat2 &lt;- as.Date(ISOdate(1960 + (months_since_1960 %/% 12), months_since_1960 %% 12 + 1, 1))
dat2 
## [1] &quot;1960-01-01&quot; &quot;1960-02-01&quot; &quot;1961-01-01&quot; &quot;1961-02-01&quot; &quot;1962-01-01&quot; &quot;1962-02-01&quot;

huangapple
  • 本文由 发表于 2023年3月4日 03:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定