I want to select columns and rename them based on the names of the columns which I keep in a separate dataframe.
This is the original dataset:
df <- tribble(
~year, ~country, ~series1, ~series2,
2003, "USA", 8, 5,
2004, "USA", 9, 6,
2005, "USA", 11, 7,
2006, "USA", 10, 8
)
I want to select and rename two columns and I want to specify that like this:
specs <- tribble(
~old_name, ~new_name,
"country", "region",
"series1", "gdp_growth"
)
I want this result:
expected_df <- tribble(
~region, ~gdp_growth,
"USA", 8,
"USA", 9,
"USA", 11,
"USA", 10
)
This does not work:
df %>%
select(specs$new_name = specs$old_name)
Error: unexpected '=' in: "df %>% select(specs$new_name ="