Reputation: 69
I have a variable_list
and a country_list
that I want to download data for. I'd like to automate that as I don't want to push each download on it's own.
I want the code to iterate through the variable_list
, download data for the respective country_list
and store them as "variable"_data
. I use get_eurostat for the downloads.
I tried:
variable_list <- list("demo_r_d3area", "demo_r_d3dens", "demo_r_fagec")
country_list <- list("AT","BE","DK")
for (i in variable_list) {
(assign(paste("data_", i, sep = ""), variable_list[[i]])) <-
get_eurostat(id = variable_list, time_format = "num",
filters = list(geo = country_list))
}
But I don't think I'm heading into the right direction with that.
Upvotes: 0
Views: 186
Reputation: 76402
I believe the right way to read in the data is with a lapply
loop into a list.
library(eurostat)
variable_list <- list("demo_r_d3area", "demo_r_d3dens", "demo_r_fagec")
country_list <- list("AT","BE","DK")
data_list <- lapply(variable_list, function(v){
get_eurostat(
id = v,
time_format = "num",
filters = list(geo = country_list)
)
})
names(data_list) <- variable_list
str(data_list)
#List of 3
# $ demo_r_d3area: tibble [156 × 5] (S3: tbl_df/tbl/data.frame)
# ..$ unit : chr [1:156] "KM2" "KM2" "KM2" "KM2" ...
# ..$ landuse: chr [1:156] "L0008" "L0008" "L0008" "L0008" ...
# ..$ geo : chr [1:156] "AT" "AT" "AT" "AT" ...
# ..$ time : num [1:156] 1990 1991 1992 1993 1994 ...
# ..$ values : num [1:156] 82528 82528 82523 82518 82512 ...
# $ demo_r_d3dens: tibble [90 × 4] (S3: tbl_df/tbl/data.frame)
# ..$ unit : chr [1:90] "PER_KM2" "PER_KM2" "PER_KM2" "PER_KM2" ...
# ..$ geo : chr [1:90] "AT" "AT" "AT" "AT" ...
# ..$ time : num [1:90] 1990 1991 1992 1993 1994 ...
# ..$ values: num [1:90] 93 94 95 95.8 96.2 96.3 96.5 96.6 96.7 96.9 ...
# $ demo_r_fagec : tibble [3,780 × 5] (S3: tbl_df/tbl/data.frame)
# ..$ unit : chr [1:3780] "NR" "NR" "NR" "NR" ...
# ..$ age : chr [1:3780] "TOTAL" "TOTAL" "TOTAL" "TOTAL" ...
# ..$ geo : chr [1:3780] "AT" "AT" "AT" "AT" ...
# ..$ time : num [1:3780] 1990 1991 1992 1993 1994 ...
# ..$ values: int [1:3780] 90454 94629 95302 95227 92415 88669 88809 84045 81233 78138 ...
The data sets can now be accessed with the standard extractor operators. Here is an example of accessing demo_r_d3area
.
head(data_list$demo_r_d3area)
## A tibble: 6 x 5
# unit landuse geo time values
# <chr> <chr> <chr> <dbl> <dbl>
#1 KM2 L0008 AT 1990 82528.
#2 KM2 L0008 AT 1991 82528.
#3 KM2 L0008 AT 1992 82523.
#4 KM2 L0008 AT 1993 82518.
#5 KM2 L0008 AT 1994 82512.
#6 KM2 L0008 AT 1995 82519.
Upvotes: 2
Reputation: 16978
Are you trying to do this?
for (i in variable_list) {
variable_list[[i]] <- get_eurostat(
id = i,
time_format = "num",
filters = list(geo = country_list)
)
assign(paste("data_", i, sep = ""), variable_list[[i]])
}
If this is not what you are trying to do, please clearify your question.
Upvotes: 1