R 将表达式传递给聚合密钥

R 将表达式传递给聚合密钥,r,R,我很容易做到: library(tsibble) tourism %>% aggregate_key(Purpose * (State / Region), Trips = sum(Trips)) 假设我有 string <- 'Purpose * (State / Region)' 一些函数可以是什么 感谢您的帮助我们可以使用parse\u expr aggregate_key(tourism, .spec = !!rlang::parse_expr(string), T

我很容易做到:

library(tsibble)
tourism %>% 
  aggregate_key(Purpose * (State / Region), Trips = sum(Trips))
假设我有

string <- 'Purpose * (State / Region)'
一些函数可以是什么


感谢您的帮助

我们可以使用
parse\u expr

aggregate_key(tourism, .spec = !!rlang::parse_expr(string), Trips = sum(Trips))
# A tsibble: 34,000 x 5 [1Q]
# Key:       Purpose, State, Region [425]
#   Quarter Purpose      State        Region        Trips
#     <qtr> <chr*>       <chr*>       <chr*>        <dbl>
# 1 1998 Q1 <aggregated> <aggregated> <aggregated> 23182.
# 2 1998 Q2 <aggregated> <aggregated> <aggregated> 20323.
# 3 1998 Q3 <aggregated> <aggregated> <aggregated> 19827.
# 4 1998 Q4 <aggregated> <aggregated> <aggregated> 20830.
# 5 1999 Q1 <aggregated> <aggregated> <aggregated> 22087.
# 6 1999 Q2 <aggregated> <aggregated> <aggregated> 21458.
# 7 1999 Q3 <aggregated> <aggregated> <aggregated> 19914.
# 8 1999 Q4 <aggregated> <aggregated> <aggregated> 20028.
# 9 2000 Q1 <aggregated> <aggregated> <aggregated> 22339.
#10 2000 Q2 <aggregated> <aggregated> <aggregated> 19941.
# … with 33,990 more rows

或者使用
glue
parse

eval(parse(text = glue::glue('tourism %>% aggregate_key({string}, Trips = sum(Trips))')))
# A tsibble: 34,000 x 5 [1Q]
# Key:       Purpose, State, Region [425]
   Quarter Purpose      State        Region        Trips
     <qtr> <chr*>       <chr*>       <chr*>        <dbl>
 1 1998 Q1 <aggregated> <aggregated> <aggregated> 23182.
 2 1998 Q2 <aggregated> <aggregated> <aggregated> 20323.
 3 1998 Q3 <aggregated> <aggregated> <aggregated> 19827.
 4 1998 Q4 <aggregated> <aggregated> <aggregated> 20830.
 5 1999 Q1 <aggregated> <aggregated> <aggregated> 22087.
 6 1999 Q2 <aggregated> <aggregated> <aggregated> 21458.
 7 1999 Q3 <aggregated> <aggregated> <aggregated> 19914.
 8 1999 Q4 <aggregated> <aggregated> <aggregated> 20028.
 9 2000 Q1 <aggregated> <aggregated> <aggregated> 22339.
10 2000 Q2 <aggregated> <aggregated> <aggregated> 19941.
# … with 33,990 more rows
eval(解析(text=glue::glue('tourism%>%aggregate_key({string},Trips=sum(Trips))))
#一个tsibble:34000x5[1Q]
#关键词:目的、州、地区[425]
四分之一目的州地区旅行
1 1998年第一季度23182。
2 1998年第2季度20323。
3 1998年第3季度19827。
1998年第4季度20830。
5 1999年第一季度22087。
6 1999年第2季度21458。
一九九九年第七季一九九四年第三季。
8 1999年4月28日。
9 2000年第一季度22339。
10 2000年第2季度19941。
#…还有33990行

它似乎不起作用``R>library(tsibble)R>string tourism%%>%+aggregate\u key(as.formula(string),Trips=sum(Trips))错误:在
group\u by()中添加计算列时出现问题。
mutate()
column
as.formula(string)
存在x问题。ℹ
as.formula(string)=agg_vec(
as.formula(string)
。找不到x对象“as.formula(string)”,请运行
rlang::last_error()
查看错误发生的位置```fabletools::聚合_key@Andrea我想知道更新是否有助于您感谢您的帮助,但是
eval parse
应该是一个极端的选择。我正在寻找一种在标准评估范围内与fable合作的方法frame@Andrea好的,我添加了
parse\u expr
。希望它能起作用
tourism %>%
    aggregate_key( .spec = !!rlang::parse_expr(string), Trips = sum(Trips))
eval(parse(text = glue::glue('tourism %>% aggregate_key({string}, Trips = sum(Trips))')))
# A tsibble: 34,000 x 5 [1Q]
# Key:       Purpose, State, Region [425]
   Quarter Purpose      State        Region        Trips
     <qtr> <chr*>       <chr*>       <chr*>        <dbl>
 1 1998 Q1 <aggregated> <aggregated> <aggregated> 23182.
 2 1998 Q2 <aggregated> <aggregated> <aggregated> 20323.
 3 1998 Q3 <aggregated> <aggregated> <aggregated> 19827.
 4 1998 Q4 <aggregated> <aggregated> <aggregated> 20830.
 5 1999 Q1 <aggregated> <aggregated> <aggregated> 22087.
 6 1999 Q2 <aggregated> <aggregated> <aggregated> 21458.
 7 1999 Q3 <aggregated> <aggregated> <aggregated> 19914.
 8 1999 Q4 <aggregated> <aggregated> <aggregated> 20028.
 9 2000 Q1 <aggregated> <aggregated> <aggregated> 22339.
10 2000 Q2 <aggregated> <aggregated> <aggregated> 19941.
# … with 33,990 more rows