R & dplyr's select not removing columns after various transforms? -
this question has answer here:
it looks select isn't removing un-selected columns data-set.. quite odd. here simple example :
library(nycflights13) library(dplyr) dly <- flights %>% group_by( year, month, day) %>% summarise( arr_mean = mean(arr_delay, na.rm=true), dep_mean = mean(dep_delay, na.rm=true) ) %>% mutate( dt = as.date(isodate( year, month, day ) ) ) > glimpse( dly, 50 ) observations: 365 variables: 6 $ year (int) 2013, 2013, 2013, 2013, 2013... $ month (int) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1... $ day (int) 1, 2, 3, 4, 5, 6, 7, 8, 9, 1... $ arr_mean (dbl) 12.6510229, 12.6928879, 5.73... $ dep_mean (dbl) 11.548926, 13.858824, 10.987... $ dt (date) 2013-01-01, 2013-01-02, 201...
so... simple.. mean day, , add r date. (yes, know there time_hour in data set, should still work). want rid of year, month & day fields (to gather() ggplot)... select isn't striping them out :
dly %>% select( dt, arr_mean, dep_mean) %>% glimpse(50) observations: 365 variables: 5 $ year (int) 2013, 2013, 2013, 2013, 2013... $ month (int) 1, 1, 1, 1, 1, 1, 1, 1, 1, 1... $ dt (date) 2013-01-01, 2013-01-02, 201... $ arr_mean (dbl) 12.6510229, 12.6928879, 5.73... $ dep_mean (dbl) 11.548926, 13.858824, 10.987...
day gone, year , month still there. why?
even if romp on them, still there :
dly$year <- null dly$month <- null dly$day <- null dly %>% glimpse(50) observations: 365 variables: 3 $ arr_mean (dbl) 12.6510229, 12.6928879, 5.73... $ dep_mean (dbl) 11.548926, 13.858824, 10.987... $ dt (date) 2013-01-01, 2013-01-02, 201...
seems gone, arn't :
dly %>% select( dep_mean) %>% glimpse(50) error: invalid column index : na variable: year = year
i'm sure i'm missing obvious, i'm not sure what.
if don't group_by / mutate data works fine.
thanks in advance help
if run dly%>%head()
, you'll see in console dly
still grouped :
groups: year, month [1]
insert %>% ungroup()%>%
before selecting should "free" dly