[英]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。
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.Date
的frac=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)))
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.