R switch statement on comparisons
Asked Answered
S

7

16

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"
)
Serg answered 11/9, 2012 at 23:4 Comment(2)
Don't know if it helps, but I proposed an edit for your "ugly" code that makes it a lot less ugly.Mysterious
A solution using switch() and match() was given here (much later in 2014): https://mcmap.net/q/747875/-switch-statement-is-not-working-for-numerical-objectsDiggings
P
20

Here is a similar answer to Josh's, but using findInterval:

Length <- 0:11

cuts <- c(-Inf, 1, 5, 10, Inf)
labs <- c("Walk", "bike", "drive", "fly")

labs[findInterval(Length, cuts)]
# [1] "Walk"  "bike"  "bike"  "bike"  "bike"  "drive" "drive"
# [8] "drive" "drive" "drive" "fly"   "fly"

You can also use nested ifelse statements, it's a matter of taste:

ifelse(Length < 1,  "Walk",
ifelse(Length < 5,  "bike",
ifelse(Length < 10, "drive",
                    "fly")))
# [1] "Walk"  "bike"  "bike"  "bike"  "bike"  "drive" "drive"
# [8] "drive" "drive" "drive" "fly"   "fly"
Propylite answered 11/9, 2012 at 23:21 Comment(1)
OK, that works, thanks! and it does seem a bit more readable than the mess I had.Serg
L
13

Using dplyr's case_when statement:

library(dplyr)
Length <- 3.5
mode <- case_when(
                Length < 1 ~ "Walk",
                1 <= Length & Length < 5 ~ "bike",
                5 <= Length & Length < 10 ~ "drive",
                Length >= 10 ~ "fly"
          )
mode
#> [1] "bike"
Luckin answered 10/7, 2017 at 11:2 Comment(0)
T
7

Would cut() do what you need?

Length <- 0:11

cuts <- c(-Inf, 1, 5, 10, Inf)
labs <- c("Walk", "bike", "drive", "fly")

as.character(cut(Length, breaks = cuts, labels = labs, include.lowest=TRUE))
#  [1] "Walk"  "Walk"  "bike"  "bike"  "bike"  "bike"  "drive" "drive" "drive"
# [10] "drive" "drive" "fly"  
Tourneur answered 11/9, 2012 at 23:9 Comment(2)
This would work great for the simplified example I posted, but maybe not as well for what I really want to do... Instead of just choosing a label, I need each case to evaluate a separate equation.Serg
@Serg -- But now you have a character vector, which you can feed to switch()...Paddle
C
2

Cut and Switch: Use the factor level from cut() and pass into switch() to return appropriate code.

 transport <- function(dist) {
   stopifnot(is.numeric(dist))
   x <- as.numeric(cut(dist, c(-Inf,0,1,5,10,Inf)), right = TRUE)
   switch (x,
           "No distance",
           "Walk",
           "Bike",
           "Drive",
           "Fly",
           stop("Not sure")
   )
 }
Cufic answered 29/8, 2018 at 22:19 Comment(0)
C
0

This answer would be an overkill for the example at hand but it is very flexible -- you can do anything inside evaluate_like_this().

evaluate_one <- function(x) {
  switch(
    x,
    evaluate_like_this(x, "Walk"),
    evaluate_like_this(x, "No distance"),
    evaluate_like_this(x, "Bike"),
    evaluate_like_this(x, "Drive"),
    evaluate_like_this(x, "Fly")
  )
}

evaluate_like_this <- function(x, y) {
  paste0(x, ": ", y)
}

these_data <- 0:11
intervals <- cut(these_data, breaks = c(-Inf, 1, 5, 10, Inf), label = FALSE)
unlist(lapply(intervals, evaluate_one))
#>  [1] "1: Walk"        "1: Walk"        "2: No distance" "2: No distance"
#>  [5] "2: No distance" "2: No distance" "3: Bike"        "3: Bike"       
#>  [9] "3: Bike"        "3: Bike"        "3: Bike"        "4: Drive"

Created on 2018-12-19 by the reprex package (v0.2.1)

Cruller answered 20/12, 2018 at 2:52 Comment(0)
L
0
length <- 3.5
cuts <- c( 1, 5, 10, Inf )
labs <- c( "Walk", "bike", "drive", "fly" )
x <- which( cuts > length )[1]
switch(x,{labs[1]},{labs[2]},{labs[3]},{labs[4]})
Lido answered 23/10, 2021 at 4:11 Comment(1)
This answer was flagged as Low Quality and could benefit from an explanation. Here are some guidelines for How do I write a good answer?. Code only answers are not considered good answers and are likely to be downvoted and/or deleted because they are less useful to a community of learners. It's only obvious to you. Explain what it does, and how it's different / better than existing answers. From ReviewNovella
F
0

I think this is a bit tidier, and no longer than the desired switch statement.

transportMode <- function(length){
   if(length < 1) return("Walk")
   if(length < 5) return("Bike")
   if(length < 10) return("Drive")
   return("Fly")
}
transportMode(8.2)
[1] "Drive"
transportMode(3.5)
[1] "Bike"
Femininity answered 30/6, 2023 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.