I'm trying to write an R script to evaluate different expressions based on fitting a value within ranges. The idea is that if Length is within one range, it will get evaluated one way, and if it's in a longer range it will get evaluated differently.
I can make this work with if/else statements, but it's pretty ugly, and I'm sure there must be a better way... here's code that works.
Length=8.2
if (Length<1)
mode="Walk"
else if (1<=Length & Length <5)
mode="bike"
else if (5<=Length & Length <10)
mode="drive"
else if (Length>=10)
mode="fly"
I've been trying to make something work with the switch function, but it seems to only work with text or integers... is there a way to have a switch statement that conducts evaluations at each case such as this?
Length=3.5
switch(Length,
(Length<1) mode="Walk"
(1<=Length & Length <5) mode="bike"
(5<=Length & Length <10) mode="drive"
(Length=>10) mode="fly"
)
switch()
andmatch()
was given here (much later in 2014): https://mcmap.net/q/747875/-switch-statement-is-not-working-for-numerical-objects – Diggings