如何删除 tsibble 对象聚合中的时间分量?
How to remove the temporal component in an aggregation of a tsibble object?
我一直在使用 tsibble
包,但我不知道如何从聚合结果中删除时间分量的正确方法。
所以在下面的数据集中,我想要按地区和州划分平均行程。是将 tsibble
转换为 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
如果我们在 tourism
数据上使用 select(-Quarter)
,它会给出一条信息性错误消息。
library(tsibble)
library(dplyr)
tourism %>% select(-Quarter)
Error: Column Quarter
(index) can't be removed. Do you need as_tibble()
to work with data frame?
因此,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
#...
为了完整起见:
来自 tsibble
的 reference manual
Column-wise verbs, including select()
, transmute()
, summarise()
,
mutate()
& transmute()
, keep the time context hanging around. That is,
the index variable cannot be dropped for a tsibble. If any key
variable is changed, it will validate whether it’s a tsibble
internally. Use as_tibble()
to leave off the time context.
不能删除时间分量,as_tibble()
是转换为 tibble
的正确选择。
我一直在使用 tsibble
包,但我不知道如何从聚合结果中删除时间分量的正确方法。
所以在下面的数据集中,我想要按地区和州划分平均行程。是将 tsibble
转换为 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
如果我们在 tourism
数据上使用 select(-Quarter)
,它会给出一条信息性错误消息。
library(tsibble)
library(dplyr)
tourism %>% select(-Quarter)
Error: Column
Quarter
(index) can't be removed. Do you needas_tibble()
to work with data frame?
因此,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
#...
为了完整起见:
来自 tsibble
Column-wise verbs, including
select()
,transmute()
,summarise()
,mutate()
&transmute()
, keep the time context hanging around. That is, the index variable cannot be dropped for a tsibble. If any key variable is changed, it will validate whether it’s a tsibble internally. Useas_tibble()
to leave off the time context.
不能删除时间分量,as_tibble()
是转换为 tibble
的正确选择。