16

我正在尝试编写一个 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"
)
4

6 回答 6

20

这是与 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"
于 2012-09-11T23:21:48.443 回答
12

使用 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"
于 2017-07-10T11:02:55.580 回答
7

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"  
于 2012-09-11T23:09:34.507 回答
2

剪切和切换:使用 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")
   )
 }
于 2018-08-29T22:19:45.827 回答
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 包(v0.2.1)于 2018 年 12 月 19 日创建

于 2018-12-20T02:52:37.827 回答
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]})
于 2021-10-23T04:11:54.900 回答