Can you give me a hint how can I deal with a problem: I have a data set and I want to apply a long code I have (with loops and stuff)... How can I write a for loop to run through the code separately by a subset (for example by city variable that I have in the data set) and then save the results by city. Thanks!
I have tried the following approach but it is not working
library(dplyr)
library(purr)
cities <- unique(df$city)
my_function <- function(df){
## wrap your entire code here
}
map_df(cities, function(df, cityname){
df %>%
filter(city == cityname) %>%
my_function()%>%
write.csv(paste0(cityname, ".csv"))
}
)
Error in filter_impl(.data, quo) : Result must have length 9059907, not 49
My df contains 9059907 observations and my 'city' character contains 49