How to iteratively build and export multiple graphs from 1 data set based on a factor

Here is a small example of how it could be done using for loops with the mtcars dataset for(g in unique(mtcars$gear)) { f <- filter(mtcars, gear == g) p <- ggplot(f, aes(disp, hp)) + geom_point() ggsave(paste0(‘plot_’, g,’.jpg’), p) } In your case it would something like this for(s in unique(WQ_byStation$StationName)){ f <- filter(WQ_byStation, StationName == … Read more

Iteration over list slices

If you want to divide a list into slices you can use this trick: list_of_slices = zip(*(iter(the_list),) * slice_size) For example >>> zip(*(iter(range(10)),) * 3) [(0, 1, 2), (3, 4, 5), (6, 7, 8)] If the number of items is not dividable by the slice size and you want to pad the list with None … Read more

What is the meaning of list[:] in this code? [duplicate]

This is one of the gotchas! of python, that can escape beginners. The words[:] is the magic sauce here. Observe: >>> words = [‘cat’, ‘window’, ‘defenestrate’] >>> words2 = words[:] >>> words2.insert(0, ‘hello’) >>> words2 [‘hello’, ‘cat’, ‘window’, ‘defenestrate’] >>> words [‘cat’, ‘window’, ‘defenestrate’] And now without the [:]: >>> words = [‘cat’, ‘window’, ‘defenestrate’] … Read more

Fastest way to iterate an Array in Java: loop variable vs enhanced for statement [duplicate]

If you’re looping through an array, it shouldn’t matter – the enhanced for loop uses array accesses anyway. For example, consider this code: public static void main(String[] args) { for (String x : args) { System.out.println(x); } } When decompiled with javap -c Test we get (for the main method): public static void main(java.lang.String[]); Code: … Read more

How to iterate over a string in C?

You want: for (i = 0; i < strlen(source); i++) { sizeof gives you the size of the pointer, not the string. However, it would have worked if you had declared the pointer as an array: char source[] = “This is an example.”; but if you pass the array to function, that too will decay … Read more