myClone
myClone

Reputation: 1749

Defining multiple ranges with an if statement in R

I have a situation where I have a table called districts with a column called zone, for which 410 values, numbers 1-410 are stored...

Currently, the R code is structured like this:

# add II data to the external matrix
zoneNames <- districts$zone
maxExternalZoneNum <- 99 #assumes externals first in zone numbering and are less than 99
vehicle[zoneNames > maxExternalZoneNum, zoneNames > maxExternalZoneNum] <- get(paste(p, "vehicle", sep=""))[zoneNames > maxExternalZoneNum, zoneNames > maxExternalZoneNum]
assign(paste(p, "vehicle", sep=""),vehicle)
rm(vehicle)

Except that for my purpose, the 99 maxExternalZoneNum does not apply. I have 1-30 external zone numbers plus another external zone number, 410.

31-409 are "internal" zones, and cannot be renumbered. So I must define maxExternalZoneNum as a range of values, 1:30 and 410.

What methods could I deploy to efficiently handle the change in the maxExternalZoneNum so that it does not break the execution of this code?

Upvotes: 1

Views: 3865

Answers (1)

Metrics
Metrics

Reputation: 15458

district$zone<-with(district,ifelse((zone<=30|zone==410),"external","internal")

Or you can use %in% as suggested by Frank:

district$zone<-with(district,ifelse((zone %in% c(1:30,410)),"external","internal")

Upvotes: 1

Related Questions