简体   繁体   English

R 数据框 - 添加具有年度季度开始日期的新列

[英]R dataframe - adding new column with starting date of year quarter

I have a dataframe like:我有一个数据框,如:

yq       store   value    
2014 Q1  1000    89

How can I add an extra column which contains, based on the year quarter in column 1 , the starting date of that quarter.如何添加一个额外的列,其中包含基于第 1 列中的年度季度,该季度的开始日期。 So in this example I would like to add: 01-01-2014.所以在这个例子中,我想添加:01-01-2014。

For instance, you could use lubridate :例如,您可以使用lubridate

library(tidyverse)
df <- tibble(yq = "2014 Q1", store = 1e3, value = 89)

df_new <- df %>% 
    mutate(start_date = lubridate::yq(yq))

Results结果

# A tibble: 1 x 4
  yq      store value start_date
  <chr>   <dbl> <dbl> <date>    
1 2014 Q1  1000    89 2014-01-01

Assuming that the data is as in the Note at the end, first convert yq to a yearqtr object.假设数据和最后的Note一样,先把yq转成yearqtr对象。 Such objects are represented internally as year + frac where frac = 0, 1/4, 2/4 and 3/4 for Q1, Q2, Q3 and Q4.此类对象在内部表示为 year + frac,其中 Q1、Q2、Q3 和 Q4 的 frac = 0、1/4、2/4 和 3/4。 They render as shown below and can be readily manipulated, eg yq + 1/4 is the next quarter.它们呈现如下所示并且可以很容易地操作,例如yq + 1/4是下一个季度。 See ?yearqtr for more information.有关更多信息,请参阅?yearqtr

Given the yearqtr object you may not even need the Date column;鉴于yearqtr对象,您甚至可能不需要Date列; however, if you do then convert that to Date class.但是,如果您这样做,则将其转换为Date类。 The default conversion is to give the first of the quarter.默认转换是给出季度的第一个。 (Use the frac=1 argument of as.Date to get the end of the quarter.) (使用as.Datefrac=1参数来获取季度末。)

library(zoo)
transform(transform(DF, yq = as.yearqtr(yq)), Date = as.Date(yq))

giving:给予:

       yq store value       Date
1 2014 Q1  1000    89 2014-01-01

If you don't need yq as a yearqtr object then it could be shortened slightly:如果您不需要yq作为yearqtr对象,则可以稍微缩短它:

transform(DF, Date = as.Date(as.yearqtr(yq)))

Note笔记

The input shown reproducibly is assumed to be:可重复显示的输入假定为:

DF <- structure(list(yq = structure(1L, .Label = "2014 Q1", class = "factor"), 
    store = 1000L, value = 89L), class = "data.frame", row.names = c(NA, 
-1L))

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

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