R 为什么在(年、月)之前跟随一个组的变异似乎遗漏了一行?

R 为什么在(年、月)之前跟随一个组的变异似乎遗漏了一行?,r,dplyr,tidyr,mutate,summarize,R,Dplyr,Tidyr,Mutate,Summarize,我有一个每日周期的数据框架,我正在将其转换为每月周期,包括基于总结值的简单转换: tibble( date = ymd("2002-12-31") + c(0:60), index = 406 * exp(cumsum(rnorm(61,0,0.01))) ) %>% mutate( year = year(date), month = month(date) ) %>% group_by(year, month) %>% summarise

我有一个每日周期的数据框架,我正在将其转换为每月周期,包括基于总结值的简单转换:

tibble(
  date = ymd("2002-12-31") + c(0:60),
  index = 406 * exp(cumsum(rnorm(61,0,0.01)))
) %>% mutate(
  year = year(date),
  month = month(date)
) %>% group_by(year, month) %>% summarise(
  date = last(date),
  month.close = last(index),
) %>% mutate(
  month.change = log(month.close / lag(month.close))
)
代码看起来很简单,但当我运行它时,我得到了一些奇怪的东西:

`summarise()` regrouping output by 'year' (override with `.groups` argument)
# A tibble: 4 x 5
# Groups:   year [2]
   year month date       month.close month.change
  <dbl> <dbl> <date>           <dbl>        <dbl>
1  2002    12 2002-12-31        403.     NA      
2  2003     1 2003-01-31        419.     NA      
3  2003     2 2003-02-28        422.      0.00572
4  2003     3 2003-03-01        417.     -0.0121 
返回预期结果

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 4 x 4
  year.month date       month.close month.change
  <yearmon>  <date>           <dbl>        <dbl>
1 Dec 2002   2002-12-31        405.     NA      
2 Jan 2003   2003-01-31        428.      0.0560 
3 Feb 2003   2003-02-28        421.     -0.0173 
4 Mar 2003   2003-03-01        423.      0.00513
`summary()`解组输出(用`.groups`参数覆盖)
#一个tibble:4x4
年.月日期月.结束月.更改
2002年12月1日2002年12月31日405。NA
2003年1月2日2003年01月31日428。0.0560
2003年2月3日2003年02月28日421-0.0173
2003年3月4日2003-03-01 423。0.00513

我遗漏了什么?

当您将
groupby
summary
一起使用时,默认情况下只删除最后一级分组

因此,在这个阶段,您的数据仍然按
年份进行分组

tibble(
  date = ymd("2002-12-31") + c(0:60),
  index = 406 * exp(cumsum(rnorm(61,0,0.01)))
) %>% mutate(
  year = year(date),
  month = month(date)
) %>% group_by(year, month) %>% summarise(
  date = last(date),
  month.close = last(index))

# A tibble: 4 x 4
# Groups:   year [2] # <- Notice this
#   year month date       month.close
#  <int> <int> <date>           <dbl>
#1  2002    12 2002-12-31        411.
#2  2003     1 2003-01-31        393.
#3  2003     2 2003-02-28        406.
#4  2003     3 2003-03-01        398.

对于第二步,由于您的数据仅按一个键分组,因此会在
摘要
之后删除数据,并获得预期的输出。

当您将
分组依据
摘要
一起使用时,默认情况下只会删除最后一级分组

因此,在这个阶段,您的数据仍然按
年份进行分组

tibble(
  date = ymd("2002-12-31") + c(0:60),
  index = 406 * exp(cumsum(rnorm(61,0,0.01)))
) %>% mutate(
  year = year(date),
  month = month(date)
) %>% group_by(year, month) %>% summarise(
  date = last(date),
  month.close = last(index))

# A tibble: 4 x 4
# Groups:   year [2] # <- Notice this
#   year month date       month.close
#  <int> <int> <date>           <dbl>
#1  2002    12 2002-12-31        411.
#2  2003     1 2003-01-31        393.
#3  2003     2 2003-02-28        406.
#4  2003     3 2003-03-01        398.
对于第二步,由于您的数据仅按一个键分组,因此将在
summary
之后删除它,从而获得预期的输出

tibble(
  date = ymd("2002-12-31") + c(0:60),
  index = 406 * exp(cumsum(rnorm(61,0,0.01)))
) %>% mutate(
  year = year(date),
  month = month(date)
) %>% group_by(year, month) %>% summarise(
  date = last(date),
  month.close = last(index), .groups = 'drop',
) %>% mutate(
  month.change = log(month.close / lag(month.close))
)

#   year month date       month.close month.change
#  <int> <int> <date>           <dbl>        <dbl>
#1  2002    12 2002-12-31        399.    NA       
#2  2003     1 2003-01-31        380.    -0.0510  
#3  2003     2 2003-02-28        381.     0.00257 
#4  2003     3 2003-03-01        381.     0.000673