R For Loop iteration number when using non-numeric identifiers

Viewed 22

I have a for loop that creates a series of maps for each of the unique crimes types in a list. These maps are then exported to a powerpoint presentation pres_1.

for(i in unique(crime_df$crime_type)) {
  crime_map <- ggplot(data = filter(crime_df, crime_type== i)) + 
    geom_sf(aes(geometry = geometry, fill = curr_yr_count))
pres_1 <- add_slide(pres_1, layout = "Title and Content", master = "Office Theme")
pres_1 <- ph_with(pres_1, value = crime_map, location = ph_location_type(type = "body"))
}

I would like to move the map slides to elsewhere within the presentation, based on the iteration number of the loop - e.g. the first map produced should go to slide 20, the second to slide 22 etc. However, the value of i refers to the value of crime_type, which is non-numeric (e.g. robbery etc.).

Is there any way to get the numeric value of i so I could add the following line of code to the end of the loop? pres_1 <- move_slide(pres_1, index = length(pres_1), to = 22 + i)

0 Answers
Related