I have the dataset downloaded from google mobility report and the idea is to process it and group it by country and by month in order to have a more concise summary. When I for
run through all the mobility variants and export it to excel, the values appear empty or the values are all the same. I leave the script for your consideration.
library(dplyr)
library(lubridate)
library(tidyr)
library(writexl)
Global_Mobility_Report <- read.csv("C:/Global_Mobility_Report.csv")
View(Global_Mobility_Report)
Global_Mobility_Report$date <- as.Date(Global_Mobility_Report$date)
Global_Mobility_Report$mes <- month(Global_Mobility_Report$date)
names <- colnames(Global_Mobility_Report)
for (i in 9:14) {
data = paste("Global_Mobility_Report$", names[i], sep = "")
df <- Global_Mobility_Report %>%
group_by(mes, country_region) %>%
summarise(value_mean = mean(data, na.rm = TRUE))
dataset <- df %>%
spread(mes, value_mean)
nombre = paste(names[i], ".xlsx")
write_xlsx(dataset,nombre)
}
The result I expect is an excel that contains the months in the first row, and the countries in the first column. Have one tab per variable
Here's what I think is a solution, although I'm not entirely clear on the problem. Anyway it could help you solve it or understand it better.
Bookstores:
Summary of averages
Instead of using a loop and fixed column positions (9:14) I use
summarise(across())
and indicate on which columns taking advantage of the fact that the ones of interest all end in the same suffix: "from_baseline". I use the argumentna.rm = TRUE
inmean
because if there are any missing the function returnsNA
.From the way I work with the format of the object,
medias_tidy
it seems to me that it is the final format: each row is an observation (average by country and month) and each column is a variable (type of activity). However,spread
I understand that you want each column to be a month/variable combination and each row a country. If not, you could better define the expected results in the question, you can always edit it.country list
Since I think you want an excel for each country, I start by converting that data.frame into a list, in which each element is a country. With this I advance to the next step which will be to write a file by country. The problem is that since I have a list I will need an iterator to do the operations inside that list. In this case use
map()
, in which an anonymous function can be declared using the symbol~
, instead of usingfunction(x) {}
. Usingmap()
pivoting the data to make each column a combination of month and variable. I specify the name of those columns by pasting the month and value withnames_glue
.I get a list where each item is a data.frame with a single row. Are you looking for that?
write the files
To write it to disk I use
purr::iwalk()
, which is from the same family asmap()
, but instead of generating a list it only generates the side effect, in this case writing the file. I use the versioniwalk()
that allows me to simultaneously iterate over a list (argument .x) and over the names of the list (argument .y). I go over the list above and for each item I write a file and name it the name of the item in the list (the country) and the extension .csv. In this test I write .csv instead of .xlsx because I don't have the library that contains it installed, but it is more or less the same.I hope it helps you, if it does not solve it, indicate it in a comment or, better yet, specify your question better. I understand that applying iterators and handling lists has a steep learning curve, but functional programming is very powerful and by creating the objects step by step it is much easier to identify errors vs. do it inside a loop.
All the code together:
Update
From a comment by @kev is a solution to the problem better specified.