Im trying to assign the value of -1, to every number in my vector that is inbetween 2 and 5. I thought an if - then statement would work. I am having some trouble. I dont th
My preference for assigning a value to a variable based on a clearly defined numeric interval is to use base R syntax:
DF$NewVar[DF$LowerLimit <= DF$OriginalVar & DF$OriginalVar < DF$UpperLimit] = "Normal"
DF$NewVar[DF$LowerLimit < DF$OriginalVar] = "Low"
DF$NewVar[DF$OriginalVar >= DF$UpperLimit] = "High"
I think this syntax is clearer than any number of R functions, largely because the code can be quickly customized to specify inclusive vs exclusive intervals. In practice, it's quite common to encounter situations where an interval can be defined as either inclusive (i.e., [-x to +x]) or exclusive (i.e., (-x to +x)) or a combination (i.e., [-x to +x)).
Additionally, base syntax provides clarity to the code if somebody else is reviewing it later. Each unique library of functions seems to have its own peculiar and slightly different syntax to achieve the same level of specificity as clearly defining the intervals using base R syntax.