Quarter Conversion using casewhen

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

Quarter Conversion using casewhen

问题

我正在尝试创建一个函数,以使用case_when函数将月份值转换为季度。

然后,我想利用mutate()函数创建一个新的变量Qtr,并确定我在每个季度中看到多少观察值。

  1. convert_to_qtr <- function(Month) {
  2. case_when(
  3. Month == "Jan" ~ "Q1",
  4. Month == "Feb" ~ "Q1",
  5. Month == "Mar" ~ "Q1",
  6. Month == "Apr" ~ "Q2",
  7. Month == "May" ~ "Q2",
  8. Month == "Jun" ~ "Q2",
  9. Month == "Jul" ~ "Q3",
  10. Month == "Aug" ~ "Q3",
  11. Month == "Sep" ~ "Q3",
  12. Month == "Oct" ~ "Q4",
  13. Month == "Nov" ~ "Q4",
  14. Month == "Dec" ~ "Q4"
  15. )
  16. }
  17. example_months <- c("Jan", "Mar", "May", "May", "Aug", "Nov", "Nov", "Dec")
  18. convert_to_qtr(example_months)
  19. df %>%
  20. mutate(Qtr = convert_to_qtr(Month)) %>%
  21. group_by(Qtr) %>%
  22. count(Qtr)

然而,我得到的答案与我的教授的下拉框中的答案不同,所以我不确定我在我的R编码中是否做错了什么。

他看到的数字是161,071 85,588 100,227 142,651,而我看到的是152,174 165,778 205,615 174,592。

英文:

I am trying to create a function so I convert the month values to quarters using the case when function.

Then I want to leverage mutate() to create a new variable Qtr and determine how many observations I see in each quarter.

  1. convert_to_qtr &lt;- function(Month) {
  2. case_when(
  3. Month == &quot;Jan&quot; ~ &quot;Q1&quot;,
  4. Month == &quot;Feb&quot; ~ &quot;Q1&quot;,
  5. Month == &quot;Mar&quot; ~ &quot;Q1&quot;,
  6. Month == &quot;Apr&quot; ~ &quot;Q2&quot;,
  7. Month == &quot;May&quot; ~ &quot;Q2&quot;,
  8. Month == &quot;Jun&quot; ~ &quot;Q2&quot;,
  9. Month == &quot;Jul&quot; ~ &quot;Q3&quot;,
  10. Month == &quot;Aug&quot; ~ &quot;Q3&quot;,
  11. Month == &quot;Sep&quot; ~ &quot;Q3&quot;,
  12. Month == &quot;Oct&quot; ~ &quot;Q4&quot;,
  13. Month == &quot;Nov&quot; ~ &quot;Q4&quot;,
  14. Month == &quot;Dec&quot; ~ &quot;Q4&quot;
  15. )
  16. }
  17. example_months &lt;- c(&quot;Jan&quot;, &quot;Mar&quot;, &quot;May&quot;, &quot;May&quot;, &quot;Aug&quot;, &quot;Nov&quot;, &quot;Nov&quot;, &quot;Dec&quot;)
  18. convert_to_qtr(example_months)
  19. df %&gt;%
  20. mutate(Qtr = convert_to_qtr(Month)) %&gt;%
  21. group_by(Qtr) %&gt;%
  22. count(Qtr)

However I am not getting the same answer as my professor in his drop down so I am not sure if I am doing something wrong in my r coding.

He sees the numbers 161,071 85,588 100,227 142,651

I am not getting that, I see 152174 165778 205615 174592

Quarter Conversion using casewhen

答案1

得分: 0

你可以按照以下方式编写该函数:

  1. convert_to_qtr <- function(Month){
  2. setNames(paste0("Q", rep(1:4, each = 3)), month.abb)[Month]
  3. }
  4. df %>%
  5. mutate(Qtr = convert_to_qtr(Month)) %>%
  6. count(Qtr)
  7. Qtr n
  8. 1 Q1 29
  9. 2 Q2 24
  10. 3 Q3 26
  11. 4 Q4 21
英文:

You could write the function as below:

  1. convert_to_qtr &lt;- function(Month){
  2. setNames(paste0(&quot;Q&quot;, rep(1:4,each=3)), month.abb)[Month]
  3. }
  4. df %&gt;%
  5. mutate(Qtr = convert_to_qtr(Month)) %&gt;%
  6. count(Qtr)
  7. Qtr n
  8. 1 Q1 29
  9. 2 Q2 24
  10. 3 Q3 26
  11. 4 Q4 21

huangapple
  • 本文由 发表于 2023年2月18日 02:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488026.html
匿名

发表评论

匿名网友

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

确定