ropensci-archive / bomrang

:warning: ARCHIVED :warning: Australian government Bureau of Meteorology (BOM) data client for R
Other
109 stars 26 forks source link

Incompatability of "bomrang_tbl" class with dplyr functions #105

Closed Blundys closed 3 years ago

Blundys commented 4 years ago

Hi, your vignette states that the bomrang_tbl class is designed to be compatible with dplyr functions but I have found that it causes many issues. I have included several examples below:

  1. dplyr::group_by() does not preform as expected, subsequent functions such as dplyr::summarise() or dplyr::filter() return results the same as if data was not grouped
  2. If data.frame and bound together using dplyr::bind_rows() even simple functions such as head cause errors (although data can still be accessed)
library(bomrang)
library(dplyr)

Hobart <- get_current_weather("HOBART (ELLERSLIE ROAD)")
Brisbane <- get_current_weather("Brisbane")

# "grouped" data frame only gives 1 results as though ungrouped
Hobart %>% mutate(day = as.Date(local_date_time_full)) %>% group_by(day) %>% summarise(mean(air_temp ))
#>   mean(air_temp)
#> 1       13.74854
# without grouping you get the same results
Hobart %>% mutate(day = as.Date(local_date_time_full)) %>% summarise(mean(air_temp ))
#>   mean(air_temp)
#> 1       13.74854
#to corrected get groups to work we must first convert to data.frame
Hobart %>% as.data.frame() %>% mutate(day = as.Date(local_date_time_full)) %>% group_by(day) %>% summarise(mean(air_temp ))
#> # A tibble: 4 x 2
#>   day        `mean(air_temp)`
#>   <date>                <dbl>
#> 1 2019-11-10             13.5
#> 2 2019-11-11             17.3
#> 3 2019-11-12             10.8
#> 4 2019-11-13             13.5

Combined <- bind_rows(Hobart, Brisbane)
head(Combined)
#> Error in tools::toTitleCase(attr(x, "type")): 'text' must be a character vector
Combineddf <- as.data.frame(Combined)
head(Combineddf)
#>   sort_order   wmo full_name history_product local_date_time
#> 1          0 94970    Hobart        IDT60801      13/01:30pm
#> 2          1 94970    Hobart        IDT60801      13/01:13pm
#> 3          2 94970    Hobart        IDT60801      13/01:00pm
#> 4          3 94970    Hobart        IDT60801      13/12:36pm
#> 5          4 94970    Hobart        IDT60801      13/12:30pm
#> 6          5 94970    Hobart        IDT60801      13/12:11pm
#>   local_date_time_full        aifstime_utc      lat      lon apparent_t
#> 1  2019-11-13 13:30:00 2019-11-13 02:30:00 -42.8897 147.3278        5.9
#> 2  2019-11-13 13:13:00 2019-11-13 02:13:00 -42.8897 147.3278        7.1
#> 3  2019-11-13 13:00:00 2019-11-13 02:00:00 -42.8897 147.3278        9.0
#> 4  2019-11-13 12:36:00 2019-11-13 01:36:00 -42.8897 147.3278        8.6
#> 5  2019-11-13 12:30:00 2019-11-13 01:30:00 -42.8897 147.3278        9.4
#> 6  2019-11-13 12:11:00 2019-11-13 01:11:00 -42.8897 147.3278        5.2
#>   cloud cloud_base_m cloud_oktas cloud_type cloud_type_id delta_t gust_kmh
#> 1     -           NA          NA          -            NA     5.7       74
#> 2     -           NA          NA          -            NA     5.2       74
#> 3     -           NA          NA          -            NA     3.4       50
#> 4     -           NA          NA          -            NA     3.6       61
#> 5     -           NA          NA          -            NA     3.2       43
#> 6     -           NA          NA          -            NA     1.7       76
#>   gust_kt air_temp dewpt press press_msl press_qnh press_tend rain_trace
#> 1      40     15.7   3.2 996.7     996.7     996.8          -        2.8
#> 2      40     15.7   4.7 996.3     996.3     996.4          -        2.8
#> 3      27     14.8   8.1 996.2     996.2     996.3          -        2.6
#> 4      33     14.6   7.2 996.1     996.1     996.2          -        2.4
#> 5      23     14.4   7.9 996.2     996.2     996.3          -        2.4
#> 6      41     11.1   7.6 996.4     996.4     996.5          -        2.2
#>   rel_hum sea_state swell_dir_worded swell_height swell_period vis_km
#> 1      NA         -                -           NA           NA      -
#> 2      NA         -                -           NA           NA      -
#> 3      NA         -                -           NA           NA      -
#> 4      NA         -                -           NA           NA      -
#> 5      NA         -                -           NA           NA      -
#> 6      NA         -                -           NA           NA      -
#>   weather wind_dir wind_spd_kmh wind_spd_kt
#> 1       -      WSW           44          24
#> 2       -        W           39          21
#> 3       -        W           28          15
#> 4       -        W           28          15
#> 5       -        W           24          13
#> 6       -       NW           28          15

Created on 2019-11-13 by the reprex package (v0.3.0)

jonocarroll commented 4 years ago

Interesting... thanks for the report. I'll have a look into what's going on.

adamhsparks commented 4 years ago

The group_by() bug is fixed in the 0.6.1 release that I had to make to keep CRAN happy due to some failing tests on the CRAN server.

The bind_rows() bug still exists and should be fixed in the next release.

PaulMelloy commented 4 years ago

Hello, This bug is still happening for me.

install.packages("bomrang")
library(dplyr)
library(devtools)
#install_github("https://github.com/ropensci/bomrang")
library(bomrang)

dat1 <- get_historical(stationid = sweep_for_stations()[1,1])

dat1 %>%
   group_by(year) %>%
   summarise(rain.mean = mean(rainfall,na.rm = TRUE))

class(dat1)

data.frame(dat1) %>%
   group_by(year) %>%
   summarise(rain.mean = mean(rainfall,na.rm = TRUE))

dat2 <- get_current_weather(station_name = sweep_for_stations()[1,3])

dat2 %>%
   group_by(cloud) %>%
   summarise(mean_temp = mean(air_temp, na.rm = TRUE))

data.frame(dat2) %>%
   group_by(cloud) %>%
   summarise(mean_temp = mean(air_temp, na.rm = TRUE))
PaulMelloy commented 4 years ago

I updated my version of R and now it is working