我正在尝试编写一个R脚本来根据范围内的值拟合来评估不同的表达式。我们的想法是,如果长度在一个范围内,它将以一种方式进行评估,如果它在更长的范围内,它将以不同的方式进行评估。
我可以使用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语句在每种情况下进行评估,例如? / p>
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"
)
答案 0 :(得分:17)
以下是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"
答案 1 :(得分:6)
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"
答案 2 :(得分:6)
使用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"
答案 3 :(得分:1)
剪切并切换:使用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")
)
}
答案 4 :(得分:0)
这个答案对于手头的例子来说算是过高了,但是它非常灵活-您可以在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"
由reprex package(v0.2.1)于2018-12-19创建