简体   繁体   English

如果R中有9个else条件的语句

[英]If statement with 9 else conditions in R

I have a function which looks at 9 different possibilities and chooses an action accordingly having the following form: 我有一个函数,它研究9种不同的可能性,并据此选择一种具有以下形式的动作:

What I'm doing is looking up a vector and for each entry in the vector deciding 我正在做的是查找向量,并为向量中的每个条目确定

IF the value in the vector is 1 THEN start function B
IF the value in the vector is 2 THEN start function C
IF the value in the vector is 3 THEN start function D
IF the value in the vector is 4 THEN start function E

etc. 等等

I would like to write this in R. Do I just put "else" for every single case? 我想在R中写这个。是否只为每个案例加上“ else”?

I have tried switch in the following way: 我已经尝试通过以下方式进行switch

condition<-6
FUN<-function(condition){
    switch(condition,
    1 = random1(net)
    2 = random2(net)
    3 = random3(net)
    4 = random4(net)
    5 = random5(net)
    6 = random6(net)
    7 = random7(net)
    8 = random8(net)
    9 = random9(net)
    10= random10(net))
}

Where random 1 to 10 are functions using the variable 'net' 其中随机数1到10是使用变量'net'的函数

and what the switch command is trying to do is checking the value of 'condition' and if its 6 as in the above example then it runs the function: random6(net) 并且switch命令试图执行的操作是检查'condition'的值,并且是否如上例中的6那样运行函数: random6(net)

Both answers pointed you to the right tools, but this is IMHO how things ought to be written. 这两个答案都为您提供了正确的工具,但这是恕我直言,应该如何编写。 The OP and both solutions so far are creating functions that use a global variable ( net ) which is not best practice. 到目前为止,OP和这两种解决方案都在创建使用全局变量( net )的函数,这不是最佳实践。

Assuming randomX are functions of one argument net , ie: 假设randomX是一个参数net函数,即:

random1 <- function(net){ [...] }
random2 <- function(net){ [...] }
[etc.]

Then you need to do: 然后,您需要执行以下操作:

FUN <- switch(condition,
              '1' = random1,
              '2' = random2,
              [etc.])

or better: 或更好:

FUN.list <- list(random1, random2, [etc.])
FUN <- FUN.list[[condition]]

In both cases, the output is a function that takes net as an input (just like randomX ) so you can evaluate it by doing: 在这两种情况下,输出都是一个将net作为输入的函数(就像randomX一样),因此您可以通过执行以下操作对其进行评估:

FUN(net)

Also note that you can do everything in one short scoop using the second approach: 还要注意,您可以使用第二种方法一口气完成所有操作:

FUN.list[[condition]](net)

Another solution is to pack all the functions you want to call into a list randoms and then select a list item based on condition : 另一个解决方案是将要调用的所有函数打包到一个列表randoms ,然后根据condition选择一个列表项:

randoms <- list(random1, random2, random3, random4, random5, random6, random7, random8, random9, random10)
FUN <- function(condition) {
  randoms[[condition]](net)
}

Use switch function as in: 使用switch功能,如下所示:

foo <- function(condition){
  switch(condition,
         '1' = print('B'),
         '2' = print('C'),
         '3' = print('D'),
         '4' = print('E'))
}

> foo(1)
[1] "B"
> foo(2)
[1] "C"
> foo(3)
[1] "D"
> foo(4)
[1] "E"

further details are in ?switch 更多详细信息在?switch

based on your example: 根据您的示例:

condition<-6
FUN<-function(condition){
    switch(condition,
    '1' = random1(net), # Maybe you're missing some commas here
    '2' = random2(net), # and here
    '3' = random3(net), # and here
    '4' = random4(net)
    ....) # all the way to '10' = random10(net)
}

this will do the trick 这将达到目的

This works well for me: 这对我来说很好:

Foo <- function(condition){
  x <- 1:20
  switch(condition,
         '1' = mean(x),
         '2' = var(x),
         '3' = sd(x))
}

> Foo(1)
[1] 10.5
> Foo(2)
[1] 35
> Foo(3)
[1] 5.91608

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM