tidyverts / tsibble

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

Deletion of index fails silenty, but rename first works. #227

Closed IanWorthington closed 3 years ago

IanWorthington commented 3 years ago

I have a tsibble containing data indexed by date, in a column called date.

I have to change the date format for ggplot, but I want the axis to still be labelled date, so I create a new column then rename the columns. Only select(-date) fails silenty. I have to do the following instead:

mutate( date2 = as.POSIXct(date, format="%Y-%m-%d")) %>%  # change date format for ggplot
dplyr::rename( "temp"="date" ) %>%  # can't delete "date" (index, maybe?)
dplyr::select( -"temp" ) %>% # now it can be deleted
dplyr::rename( "date"="date2" ) # rename this for the plot

Is this expected? Should not select() have complained?

earowang commented 3 years ago

select() no longer complains if index has been selected or deleted, because it keeps both index and key by default as per https://github.com/tidyverts/tsibble/issues/155 request. Using select(data, -temp) to remove index silently moves the index column to the most right.

library(tsibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
pedestrian %>% 
  select(-Date_Time)
#> # A tsibble: 66,037 x 5 [1h] <Australia/Melbourne>
#> # Key:       Sensor [4]
#>    Sensor         Date        Time Count Date_Time          
#>    <chr>          <date>     <int> <int> <dttm>             
#>  1 Birrarung Marr 2015-01-01     0  1630 2015-01-01 00:00:00
#>  2 Birrarung Marr 2015-01-01     1   826 2015-01-01 01:00:00
#>  3 Birrarung Marr 2015-01-01     2   567 2015-01-01 02:00:00
#>  4 Birrarung Marr 2015-01-01     3   264 2015-01-01 03:00:00
#>  5 Birrarung Marr 2015-01-01     4   139 2015-01-01 04:00:00
#>  6 Birrarung Marr 2015-01-01     5    77 2015-01-01 05:00:00
#>  7 Birrarung Marr 2015-01-01     6    44 2015-01-01 06:00:00
#>  8 Birrarung Marr 2015-01-01     7    56 2015-01-01 07:00:00
#>  9 Birrarung Marr 2015-01-01     8   113 2015-01-01 08:00:00
#> 10 Birrarung Marr 2015-01-01     9   166 2015-01-01 09:00:00
#> # … with 66,027 more rows

Created on 2020-10-20 by the reprex package (v0.3.0)