4

我一直在使用这个tsibble包,但我不知道从聚合结果中删除时间分量的正确方法是什么。所以在下面的数据集中,我想要按地区和州划分平均行程。将 a转换tsibble为 a的正确方法是tibble(可能是,我只是不确定)还是我缺少一些选项来实现聚合?

library(tsibble)
library(dplyr)

tourism %>% group_by(Region, State) %>% summarise(Mean_trips = mean(Trips))

# A tsibble: 6,080 x 4 [1Q]
# Key:       Region, State [76]
# Groups:    Region [76]
   Region   State           Quarter Mean_trips
   <chr>    <chr>             <qtr>      <dbl>
 1 Adelaide South Australia 1998 Q1       165.
 2 Adelaide South Australia 1998 Q2       112.
 3 Adelaide South Australia 1998 Q3       148.

## This is not what I want, this is what I want:

tourism %>% as_tibble %>% group_by(Region, State) %>% summarise(Mean_trips = mean(Trips))

# A tibble: 76 x 3
# Groups:   Region [76]
   Region                     State              Mean_trips
   <chr>                      <chr>                   <dbl>
 1 Adelaide                   South Australia        143.  
 2 Adelaide Hills             South Australia          7.18
4

2 回答 2

4

如果我们select(-Quarter)tourism数据上使用它会给出一个信息性错误消息。

library(tsibble)
library(dplyr)

tourism %>% select(-Quarter)

错误:Quarter无法删除列(索引)。您需要as_tibble()使用数据框吗?

因此,as_tibble是转换为 tibble 的正确方法。

tourism %>% 
    as_tibble %>% 
    group_by(Region, State) %>% 
    summarise(Mean_trips = mean(Trips))

#   Region                     State              Mean_trips
#   <chr>                      <chr>                   <dbl>
# 1 Adelaide                   South Australia        143.  
# 2 Adelaide Hills             South Australia          7.18
# 3 Alice Springs              Northern Territory      14.2 
# 4 Australia's Coral Coast    Western Australia       47.4 
#...
于 2020-04-05T13:57:11.560 回答
3

为了完整起见:来自参考手册tsibble

列式动词,包括select(), transmute(), summarise(), mutate()& transmute(),保持时间上下文。也就是说,不能为 tsibble 删除索引变量。如果任何关键变量发生变化,它将在内部验证它是否是一个 tsibble。用于as_tibble()省略时间上下文。

时间分量不能被删除,as_tibble()是转换为tibble.

于 2020-04-05T15:15:53.073 回答