Hello,
I cannot figure out how to pass tibbles to a function. I receive the error:
no applicable method for 'extract_' applied to an object of class "character"
I also think there is a better formulation of my regex with a quantifier (e.g. {1,3} for [[:alpha:]]+, but I couldn't figure out how to make it work.
Thanks so much for any help.
#################################
# data source:
# https://rsf.org/en/ranking/2020
# Reporters without borders accessed rankings
# from 2013-2020 by changing XXXX to year
# https://rsf.org/en/ranking/XXXX
# accessed on May 3, 2020
# sample data from 2 yrs below
#################################
library(tidyverse)
rankings_2013 <- tibble::tribble(~header2013,
"0Vietnam71.78",
"1Finland6.38",
"2Netherlands6.48",
"3Norway6.52",
"4Luxembourg6.68")
rankings_2014 <- tibble::tribble(~header2014,
"1Finland6.40",
"2Netherlands6.46",
"3Norway6.52",
"4Luxembourg6.70",
"5Andorra6.82",
"6Liechtenstein7.02")
fix_df <- function(df, col, year){
df %>%
extract({{col}}, into = c("rank", "country", "score"),
regex = "(\\d+)([[:alpha:]]+.?[[:alpha:]]+.?[[:alpha:]]+.?[[:alpha:]]+)(\\d+\\.*\\d*)",
remove = TRUE,
convert = TRUE) %>%
mutate(year = as.integer({{year}}))
}
fix_df(rankings_2013, header2013, 2012)
#> # A tibble: 5 x 4
#> rank country score year
#> <int> <chr> <dbl> <int>
#> 1 0 Vietnam 71.8 2012
#> 2 1 Finland 6.38 2012
#> 3 2 Netherlands 6.48 2012
#> 4 3 Norway 6.52 2012
#> 5 4 Luxembourg 6.68 2012
fix_df(rankings_2014, header2014, 2013)
#> # A tibble: 6 x 4
#> rank country score year
#> <int> <chr> <dbl> <int>
#> 1 1 Finland 6.4 2013
#> 2 2 Netherlands 6.46 2013
#> 3 3 Norway 6.52 2013
#> 4 4 Luxembourg 6.7 2013
#> 5 5 Andorra 6.82 2013
#> 6 6 Liechtenstein 7.02 2013
df_in <- ls(pattern = "rankings")
df_in
#> [1] "rankings_2013" "rankings_2014"
year <- 2012:2013
year_in <- paste(year, sep = ",")
year_in
#> [1] "2012" "2013"
col_in <- paste0("header", year + 1)
col_in
#> [1] "header2013" "header2014"
# doesn't work
# How can I pass the first argument of df_in to the function?
fix_df(df_in[1], col_in[1], year_in[1])
#> Warning: `extract_()` is deprecated as of tidyr 1.0.0.
#> Please use `extract()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
#> Error in UseMethod("extract_"): no applicable method for 'extract_' applied to an object of class "character"
# does work
fix_df(rankings_2013, col_in[1], year_in[1])
#> # A tibble: 5 x 4
#> rank country score year
#> <int> <chr> <dbl> <int>
#> 1 0 Vietnam 71.8 2012
#> 2 1 Finland 6.38 2012
#> 3 2 Netherlands 6.48 2012
#> 4 3 Norway 6.52 2012
#> 5 4 Luxembourg 6.68 2012
# end goal
# l <- list(df_in, col_in, year_in)
# pmap_dfr(l, fix_df)
Created on 2020-05-07 by the reprex package (v0.3.0)