[英]How to convert year and quarter column into date using zoo?
I'm trying to convert a data-frame column into date format.我正在尝试将数据框列转换为日期格式。 I'm using the zoo package, but can't get it to work.
我正在使用动物园 package,但无法正常工作。 The desired output is either yyyy-mm-dd, so 4 dates per year.
所需的 output 是 yyyy-mm-dd,因此每年有 4 个日期。
This is what I've tried so far:到目前为止,这是我尝试过的:
library(dplyr)
library(zoo)
as.yearqtr(1930, Q2)
as.yearqtr(1930, Q2, format = "%Y %Q%q")
To clarify.澄清。 With the following code
使用以下代码
as.yearqtr(1930, Q2, format = "%Y %Q%q") %>% as.Date()
The output is output 是
[1] "1930-01-01"
which, of course, is the 1st quarter, but it should give "1930-03-01", ie the second quarter.这当然是第一季度,但它应该给出“1930-03-01”,即第二季度。
Is this what you need?这是你需要的吗?
library(zoo)
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
test = data.frame(year = rep(2000, 4),
qtr = paste0("Q", 1:4))
test |>
mutate(
qtr.num = case_when(
qtr == "Q1" ~ 0,
qtr == "Q2" ~ 0.25,
qtr == "Q3" ~ 0.5,
qtr == "Q4" ~ 0.75),
quarter = as.yearqtr(year + qtr.num) |> as.Date())
#> year qtr qtr.num quarter
#> 1 2000 Q1 0.00 2000-01-01
#> 2 2000 Q2 0.25 2000-04-01
#> 3 2000 Q3 0.50 2000-07-01
#> 4 2000 Q4 0.75 2000-10-01
Created on 2023-01-20 with reprex v2.0.2创建于 2023-01-20,使用reprex v2.0.2
If you want a more simple solution, you may use the parse_date_time
function in lubridate
:如果你想要一个更简单的解决方案,你可以在 lubridate 中使用
parse_date_time
lubridate
:
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
test = data.frame(year = rep(2000, 4),
qtr = paste0("Q", 1:4))
test |>
mutate(
quarter = parse_date_time(paste(year, qtr), orders="%Y %q"))
#> year qtr quarter
#> 1 2000 Q1 2000-01-01
#> 2 2000 Q2 2000-04-01
#> 3 2000 Q3 2000-07-01
#> 4 2000 Q4 2000-10-01
Created on 2023-01-20 with reprex v2.0.2创建于 2023-01-20,使用reprex v2.0.2
as.yearqtr takes a single character string or vector, not two. as.yearqtr 采用单个字符串或向量,而不是两个。 Always read the the help file first to find out what the arguments are.
请始终先阅读帮助文件以找出 arguments 是什么。 Below we show producing a yearqtr object. Internally yearqtr objects are represented by the year + fraction where the fraction is 0, 1/4, 1/2, 3/4 for the 4 quarters respectively (so, for example, adding 1 gives the same quarter in the next year) and when displayed show as shown below.
下面我们展示了生成一个 yearqtr object。在内部,yearqtr 对象由年份 + 分数表示,其中分数分别为 0、1/4、1/2、3/4 的 4 个季度(因此,例如,加 1 得到明年同一季度),显示时如下所示。
library (zoo)
as.yearqtr(paste(1930, "Q2"))
## [1] "1930 Q2"
or或者
as.yearqtr(paste(1930, 2, sep = "-"))
## [1] "1930 Q2"
or或者
as.yearqtr(1930 + (2 - 1)/4)
## [1] "1930 Q2"
To get a Date class object use as.Date on the above (or just use the above as is as it directly expresses a year and quarter).要获得日期 class object,请在上面使用 as.Date(或者直接使用上面的内容,因为它直接表示年份和季度)。
as.Date(as.yearqtr(paste(1930, "Q2"))) # start of qtr
## [1] "1930-04-01"
as.Date(as.yearqtr(paste(1930, "Q2")), frac = 1) # end of qtr
## [1] "1930-06-30"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.