0

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

Alina
  • 11
  • 2
  • What exactly do you want done? It is not clear what you intend to be done by the data, nor what the data even looks like. Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample data (e.g., `dput(head(x))`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Mar 22 '19 at 19:50
  • Try `map_df(cities, function(cityname){...})` – A. Suliman Mar 22 '19 at 20:48

0 Answers0