Search code examples
rreshape2tradingquantitative-finance

Can I use a different method/function for grouped dataframe?


I have been following a tutorial where a guy was trying to pull data from wbstats into a dataframe (gross domestic product data).

I think there is something wrong with the last code segment, as I have already installed all of the required libraries (tidyverse, wbstats, data.table, plotly, psych, highcharter, quantmod, TTR, reshape2).

population <- wb('SP.POP.TOTL', country = 'countries_only') %>%
  mutate(date = as.numeric(date), value = round(value / 1000000, 2), indicator = 'Population') %>%
  select(-indicatorID)

gdp <- wb('NY.GDP.PCAP.CD', country = 'countries_only') %>%
  mutate(date = as.numeric(date), value = round(value, 2), indicator = 'GDP per Capita') %>%
  select(-indicatorID)

lifeexpectancy <- wb('SP.DYN.LE00.IN', country = 'countries_only') %>%
  mutate(date = as.numeric(date), value = round(value, 2), indicator = 'Life Expectancy') %>%
  select(-indicatorID)

df <- gdp %>%
  rbind(lifeexpectancy) %>%
  rbind(population) %>%
  data.table::dcast(... ~ indicator, value.var = 'value') %>%
  na.omit()

Here is the error for the last code segment:

Show in New Window wb() was deprecated in wbstats 1.0.0. Please use wb_data() instead.wb() was deprecated in wbstats 1.0.0. Please use wb_data() instead.wb() was deprecated in wbstats 1.0.0. Please use wb_data() instead.The dcast generic in data.table has been passed a data.frame and will attempt to redirect to the reshape2::dcast; please note that reshape2 is deprecated, and this redirection is now deprecated as well. Please do this redirection yourself like reshape2::dcast(.). In the next version, this warning will become an error.

The problem is that when I actually try to modify wb for wb_data it immediately returns me an error with the mutate values [rlang::last_error()]. I would really like to use this function, but perhaps there is a better way to it with tidyr. Any ideas?


Solution

  • If we need a tidy version, instead of rbind, use bind_rows and then change the dcast to pivot_wider

    library(dplyr)
    library(tidyr)
    library(data.table)
    df <- gdp %>%
        bind_rows(., lifeexpectancy, population)  %>%
        mutate(rn = rowid(indicator)) %>% # in case of any duplicates
        tidyr::pivot_wider(names_from = 'indicator', values_from = 'value') %>% 
        na.omit() %>%
        select(-rn)