1
votes

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

1
Try sapply(breakpoints, function(i)ifelse(wnv$Longitude > i, 'East', 'West'))Sotos
Excellent. Thank you. That works- I'll just have to do a quick cbind to add add the results to the initial dataframe.bkube

1 Answers

1
votes

I've included different regions for each breakpoint, in case that is desired. If not, just remove regions and replace the the regions[[k]][1] and regions[[k]][2] with your desired values.

breakpoints <- c(-171, 70, 5)
col_names <- paste("gt_breakpoint", seq_along(breakpoints), sep = "_")
wnv <- data.frame(longitude = c(50, -99, 143, 90, 2, -8))
regions <- list(c("region1A", "region1B"), c("region2A", "region2B"),
                c("region3A", "region3B"))
for (k in (seq_along(breakpoints))) {
  wnv[[col_names[k]]] <- ifelse(wnv$longitude > breakpoints[k], regions[[k]][1],
                               regions[[k]][2])
}
wnv
#>   longitude gt_breakpoint_1 gt_breakpoint_2 gt_breakpoint_3
#> 1        50        region1A        region2B        region3A
#> 2       -99        region1A        region2B        region3B
#> 3       143        region1A        region2A        region3A
#> 4        90        region1A        region2A        region3A
#> 5         2        region1A        region2B        region3B
#> 6        -8        region1A        region2B        region3B