'switch statement with multiple conditions in r
I want to write a switch statement in r with three conditions but can't seem to get it to work. What am I doing wrong?
# assigning some values
test.type <- "p"
var.equal<- TRUE
paired <- FALSE
# preparing text for which p-value adjustment method was used
test.description <- switch(
EXPR = test.type & var.equal & paired,
"p" & TRUE & TRUE = "Student's t-test",
"p" & FALSE & TRUE = "Student's t-test",
"p" & TRUE & FALSE = "Student's t-test",
"p" & FALSE & FALSE = "Games-Howell test",
"np" & TRUE & TRUE = "Durbin-Conover test"
)
#> Error: <text>:10:23: unexpected '='
#> 9: EXPR = test.type & var.equal & paired,
#> 10: "p" & TRUE & TRUE =
#> ^
Created on 2018-11-08 by the reprex package (v0.2.1)
A simpler version of this statement with just one condition does work-
# simpler switch
(test.description <- switch(
EXPR = test.type,
"p" = "Student's t-test",
"np" = "Durbin-Conover test"
))
#> [1] "Student's t-test"
Created on 2018-11-08 by the reprex package (v0.2.1)
Solution 1:[1]
That's not how R's switch() function works. Syntactically, it's just a function call, so the selectors have to be things that can be treated as names, not expressions like "p" & TRUE & TRUE. So your first switch could switch on test.type, and then use if statements to choose values based on var.equal and paired. But it would probably look better as a sequence of if statements, like this:
test.description <-
if (test.type == "p" && !var.equal && !paired) "Games-Howell test" else
if (test.type == "p") "Student's t-test" else
if (test.type == "np" && var.equal && paired) "Durbin-Conover test" else
"Unknown combination"
Some things to note here:
- You can use
ifstatements in an expression to produce a value; this is one big statement. - If the
elsekeywords were moved to the next lines, it wouldn't work, because the code up to there is a complete statement, so theelseclauses would be left dangling. (There are exceptions to this, but don't rely on them.) - You should almost always use the scalar
&&within aniftest rather than the vector&. - Another way to format this is to put the values in braces, with the closing brace and the
elseon the next line. I like the formatting above a little better, but your preference may vary.
Solution 2:[2]
Another solution could be to use dplyr's case_when, which uses syntax more similar to your switch statements:
library(dplyr)
## initial dataframe
df <- data.frame(
test.type = c("p", "p", "p", "p", "np", "np"),
var.equal = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE),
paired = c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE)
)
## add column test.description
mutate(df,
test.description = case_when(
test.type == "p" & !var.equal & !paired ~ "Games-Howell test",
test.type == "p" ~ "Student's t-test",
test.type == "np" & var.equal & paired ~ "Durbin-Conover test",
TRUE ~ "Unknown combination"
)
)
Solution 3:[3]
Just do this:
test.type <- "p"
var.equal<- TRUE
paired <- FALSE
test.description <- switch(
EXPR = paste(test.type, var.equal, paired),
"p TRUE TRUE" = "Student's t-test",
"p FALSE TRUE" = "Student's t-test",
"p TRUE FALSE" = "Student's t-test",
"p FALSE FALSE" = "Games-Howell test",
"np TRUE TRUE" = "Durbin-Conover test"
)
Then:
test.description
[1] "Student's t-test"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2554330 |
| Solution 2 | Joris C. |
| Solution 3 | Alan Gómez |
