tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
528 stars 50 forks source link

as.ts.tbl_ts should not fail with multiple keys if they determine only one group #258

Closed Fuco1 closed 2 years ago

Fuco1 commented 3 years ago

For example working with nested tibble of tsibbles (by key) results in tsibbles with only one "group" (i.e. one combination of all the key variables). It should be allowed to transform this into a ts object because there is no ambiguity.

> tsibble(i = 1:12, q = 5, a = "x", b = "y", index = "i", key = c(a,b))
# A tsibble: 12 x 4 [1]
# Key:       a, b [1]                        ## <<<<< only one key group exists
       i     q a     b    
   <int> <dbl> <chr> <chr>
 1     1     5 x     y    
 2     2     5 x     y    
 3     3     5 x     y    
 4     4     5 x     y    
 5     5     5 x     y    
 6     6     5 x     y    
 7     7     5 x     y    
 8     8     5 x     y    
 9     9     5 x     y    
10    10     5 x     y    
11    11     5 x     y    
12    12     5 x     y    
> tsibble(i = 1:12, q = 5, a = "x", b = "y", index = "i", key = c(a,b)) %>% as.ts(q)
Error: Can't proceed with the key of multiple variables.

Same data with the key removed works:

> tsibble(i = 1:12, q = 5, a = "x", b = "y", index = "i") %>% as.ts(q)
Time Series:
Start = 1 
End = 12 
Frequency = 1 
 [1] 5 5 5 5 5 5 5 5 5 5 5 5
> tsibble(i = 1:12, q = 5, a = "x", b = "y", index = "i", key = c(a,b)) %>% as_tsibble(key = c()) %>% as.ts(q)
Time Series:
Start = 1 
End = 12 
Frequency = 1 
 [1] 5 5 5 5 5 5 5 5 5 5 5 5
earowang commented 2 years ago

thanks, fixed.