vendredi 22 mai 2020

How to loop a conditional execution across a vector (multiple ifelse scenarios) and return multiple vectors for each scenario

I have a mock dataset where I want to evaluate how break points (in longitude) influence an outcome (a designation of east vs. west).

For example, the following line of code would amend 1 column to the dataframe (labeled region) filled with "East" or "West" depending on if the value in the Longitude column is greater or less than -97.

wnv$region <- ifelse(wnv$Longitude>-97, "East", "West")

Eventually, I want to see how different thresholds (not -97) would affect another variable in the dataset. Thus, I want to loop across a vector of values-- say breakpoints <- seq(-171, -70, 5) -- and get a new vector (column in the dataset) for each value in the breakpoint sequence.

How might you do this in a loop rather than writing a new ifelse statement for each breakpoint?

Thanks

Aucun commentaire:

Enregistrer un commentaire