USGS-R / hypeRusgs

Creating some R hype by showing off advanced techniques applied to USGS science.
3 stars 5 forks source link

Do loops vs ifelse statements #9

Closed pestack closed 5 years ago

pestack commented 6 years ago

Can we discuss do loops in R and alternatives such as ifelse statements (or other alternatives). I would be particularly interested in a discussion of when one is preferable over the other and perhaps how to set up do loops.

ldecicco-USGS commented 6 years ago

I'd be happy to... can you provide either a sample data set or describe a bit what you are doing with your loops? There are many ways to avoid loops...and generally those loop-alternatives are the preferable "R" way.

pestack commented 6 years ago

Laura, I don't have a current example to share with you and sense our meeting is in 2.5 hours from now perhaps it is best to put this topic off to another time. I have struggled with this in the past but I would have to revisit that code to provide you an example. nPerhaps it is a better topic for one of our more detailed discussions where we do a "deep dive" into a given topic. If you want to show some general examples of alternatives to loops today that would be great but I'll leave that up to you at this late hour. Thanks, Paul

On Wed, Aug 29, 2018 at 12:17 AM Laura DeCicco notifications@github.com wrote:

I'd be happy to... can you provide either a sample data set or describe a bit what you are doing with your loops? There are many ways to avoid loops...and generally those loop-alternatives are the preferable "R" way.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/USGS-R/hypeRusgs/issues/9#issuecomment-416818828, or mute the thread https://github.com/notifications/unsubscribe-auth/ANbQwylpyTKTj-bWNWNdZvhiYfRlPIv0ks5uVhW7gaJpZM4WOMgK .

-- Paul Stackelberg | Coordinator, Modeling and Mapping Team National Water Quality Assessment Project U.S. Geological Survey 425 Jordan Road, Troy NY, 12180 phone: (518) 285.5652

lindsayplatt commented 6 years ago

@pestack we will be going over loops/ifelse/functions etc at the more formal training session next week. Could you email an example data set and maybe a description of what you want to do?

pestack commented 6 years ago

Hi Lindsay, I'm at a meeting this week but I'd be happy to discuss this with you next week if you'd like. In the meantime, I've attached a script and data set. I was working with a pesticide data set that has "R" and "P" codes for over 200 pesticides. I was wondering if a loop could cycle through each "R" and "P" code and perform a set of ifelse statements (or we could use case.when). I never got a loop to work so I ended up using a macro in an editor to write repetitive code for all 200+ pesticides which seemed inefficient. Let me know if you need more clarity on this. Thanks, Paul

On Thu, Sep 20, 2018 at 12:03 PM Lindsay Carr notifications@github.com wrote:

@pestack https://github.com/pestack we will be going over loops/ifelse/functions etc at the more formal training session next week. Could you email an example data set and maybe a description of what you want to do?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/USGS-R/hypeRusgs/issues/9#issuecomment-423238375, or mute the thread https://github.com/notifications/unsubscribe-auth/ANbQw9HXxAwKrygPNBaeSmpZciJAza5Qks5uc7wkgaJpZM4WOMgK .

-- Paul Stackelberg | Coordinator, Modeling and Mapping Team National Water Quality Assessment Project U.S. Geological Survey 425 Jordan Road, Troy NY, 12180 phone: (518) 285.5652

lindsayplatt commented 5 years ago

Covered in the Sep 26, 2018 webinar. See https://github.com/USGS-R/hypeRusgs/tree/master/Webinar-9-26-2018/R.

Paul's example didn't end up needing a loop - it was all taken care of using Tidyverse methods, see Laura's script: https://github.com/USGS-R/hypeRusgs/blob/master/Webinar-9-26-2018/R/PestsSummary_ldecicco.R

We did cover basic loops and functions, though: https://github.com/USGS-R/hypeRusgs/blob/master/Webinar-9-26-2018/R/basic_loops_and_functions.R