Convert the quarter date column with the last day of each quarter to the first day of each quarter, or vice versa

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

Convert the quarter date column with the last day of each quarter to the first day of each quarter, or vice versa

问题

df1:

df1 <- structure(list(date = c("2010-3-31", "2010-6-30", "2010-9-30", "2010-12-31", "2011-3-31", "2011-6-30", "2011-9-30", "2011-12-31"), value = c(24.1, 16.4, 18.5, 8.61, 12.6, 20.1, 17.9, 14.5)), class = "data.frame", row.names = c(NA, -8L))

df2:

df2 <- structure(list(date = c("2010-1-1", "2010-4-1", "2010-7-1", "2010-10-1", "2011-1-1", "2011-4-1", "2011-7-1", "2011-10-1"), value = c(24.1, 16.4, 18.5, 8.61, 12.6, 20.1, 17.9, 14.5)), class = "data.frame", row.names = c(NA, -8L))
英文:

Suppose I have the following df1 or df2, how can I use R to convert the last day of each quarter to the first day of each quarter in date column, or vice versa? Thanks.

df1:

df1 &lt;- structure(list(date = c(&quot;2010-3-31&quot;, &quot;2010-6-30&quot;, &quot;2010-9-30&quot;, 
&quot;2010-12-31&quot;, &quot;2011-3-31&quot;, &quot;2011-6-30&quot;, &quot;2011-9-30&quot;, &quot;2011-12-31&quot;
), value = c(24.1, 16.4, 18.5, 8.61, 12.6, 20.1, 17.9, 14.5)), class = &quot;data.frame&quot;, row.names = c(NA, 
-8L))

Out:

        date value
1  2010-3-31 24.10
2  2010-6-30 16.40
3  2010-9-30 18.50
4 2010-12-31  8.61
5  2011-3-31 12.60
6  2011-6-30 20.10
7  2011-9-30 17.90
8 2011-12-31 14.50

df2:

df2 &lt;- structure(list(date = c(&quot;2010-1-1&quot;, &quot;2010-4-1&quot;, &quot;2010-7-1&quot;, &quot;2010-10-1&quot;, 
&quot;2011-1-1&quot;, &quot;2011-4-1&quot;, &quot;2011-7-1&quot;, &quot;2011-10-1&quot;), value = c(24.1, 
16.4, 18.5, 8.61, 12.6, 20.1, 17.9, 14.5)), class = &quot;data.frame&quot;, row.names = c(NA, 
-8L))

Out:

      date value
1  2010-1-1 24.10
2  2010-4-1 16.40
3  2010-7-1 18.50
4 2010-10-1  8.61
5  2011-1-1 12.60
6  2011-4-1 20.10
7  2011-7-1 17.90
8 2011-10-1 14.50

答案1

得分: 1

1) 将一个yearqtr对象转换为Date类将给出该季度的第一天:

library(dplyr)
library(zoo)

df1 %>%
  mutate(date = as.Date(as.yearqtr(as.Date(date))))

结果如下:

            date value
1 2010-01-01 24.10
2 2010-04-01 16.40
3 2010-07-01 18.50
4 2010-10-01 8.61
5 2011-01-01 12.60
6 2011-04-01 20.10
7 2011-07-01 17.90
8 2011-10-01 14.50

2) 要获取季度的末尾,请注意as.Date.yearqtr有一个frac=参数,它的默认值为0(表示季度的开始),1表示季度的末尾,或任何分数值。

df2 %>%
  mutate(date = as.Date(as.yearqtr(as.Date(date)), frac = 1))

结果如下:

            date value
1 2010-03-31 24.10
2 2010-06-30 16.40
3 2010-09-30 18.50
4 2010-12-31 8.61
5 2011-03-31 12.60
6 2011-06-30 20.10
7 2011-09-30 17.90
8 2011-12-31 14.50

3) 另一种可能性是,不是将季度表示为日期,而是直接表示为yearqtr对象。 yearqtr对象内部表示为年份 + 0, 1/4, 1/2, 3/4分别代表四个季度,因此可以轻松进行操作和排序,例如,如果yq是一个yearqtr对象,yq+1/4代表下一个季度。

df1 %>%
  mutate(date = as.yearqtr(as.Date(date)))

结果如下:

         date value
1 2010 Q1 24.10
2 2010 Q2 16.40
3 2010 Q3 18.50
4 2010 Q4 8.61
5 2011 Q1 12.60
6 2011 Q2 20.10
7 2011 Q3 17.90
8 2011 Q4 14.50
英文:

1) Converting a yearqtr object to Date class will give the first of the quarter so

library(dplyr)
library(zoo)

df1 %&gt;%
  mutate(date = as.Date(as.yearqtr(as.Date(date))))

giving:

        date value
1 2010-01-01 24.10
2 2010-04-01 16.40
3 2010-07-01 18.50
4 2010-10-01  8.61
5 2011-01-01 12.60
6 2011-04-01 20.10
7 2011-07-01 17.90
8 2011-10-01 14.50

2) To get the end of quarter note that as.Date.yearqtr has a frac= argument which is 0 (default) for beginning of quarter, 1 for end of quarter or any fraction between.

df2 %&gt;%
  mutate(date = as.Date(as.yearqtr(as.Date(date)), frac = 1))

giving:

        date value
1 2010-03-31 24.10
2 2010-06-30 16.40
3 2010-09-30 18.50
4 2010-12-31  8.61
5 2011-03-31 12.60
6 2011-06-30 20.10
7 2011-09-30 17.90
8 2011-12-31 14.50

3) Another possibility is, rather than representing the quarter by a date to represent it directly as a yearqtr object. yearqtr objects are internally year + 0, 1/4, 1/2, 3/4 for the 4 quarters so they can be easily manipulated and sorted, e.g. if yq is a yearqtr object yq+1/4 is the quarter following.

df1 %&gt;%
  mutate(date = as.yearqtr(as.Date(date)))

giving:

     date value
1 2010 Q1 24.10
2 2010 Q2 16.40
3 2010 Q3 18.50
4 2010 Q4  8.61
5 2011 Q1 12.60
6 2011 Q2 20.10
7 2011 Q3 17.90
8 2011 Q4 14.50

huangapple
  • 本文由 发表于 2023年3月23日 11:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819061.html
匿名

发表评论

匿名网友

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

确定