[英]R switch statement on comparisons
我正在嘗試編寫一個 R 腳本來根據在范圍內擬合一個值來評估不同的表達式。 這個想法是,如果 Length 在一個范圍內,它將以一種方式進行評估,如果它在更長的范圍內,它將以不同的方式進行評估。
我可以使用 if/else 語句來完成這項工作,但它非常難看,而且我確信必須有更好的方法......這是有效的代碼。
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"
我一直在嘗試使用 switch 函數來做一些事情,但它似乎只適用於文本或整數......有沒有辦法讓 switch 語句在這樣的每種情況下進行評估?
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"
)
這是與 Josh 類似的答案,但使用的是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"
您還可以使用嵌套的ifelse
語句,這是一個品味問題:
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"
使用 dplyr 的case_when
語句:
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"
cut()
做你需要的嗎?
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"
剪切和切換:使用來自 cut() 的因子級別並傳入 switch() 以返回適當的代碼。
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")
)
}
這個答案對於手頭的例子來說有點矯枉過正,但它非常靈活——你可以在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"
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]})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.