【问题标题】:If statement with 9 else conditions in R在 R 中具有 9 个 else 条件的 if 语句
【发布时间】:2012-12-20 12:50:01
【问题描述】:

我有一个函数,它查看 9 种不同的可能性并相应地选择一个具有以下形式的动作:

我正在做的是查找一个向量并为向量中的每个条目决定

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

等等

我想用 R 写这个。我是否只为每个案例都加上“else”?

我已经通过以下方式尝试了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))
}

其中随机 1 到 10 是使用变量 'net' 的函数

switch 命令试图做的是检查 'condition' 的值,如果它在上面的示例中为 6,则它运行函数:random6(net)

【问题讨论】:

  • if/ifif/else if 完全不同。先决定你需要哪一个。
  • 也看?switch
  • 我已经编辑了这个问题,现在可能更清楚了
  • 如果vector中的值都是1和2,或者1和3等等,要应用什么函数??
  • @RomainFrancois 有答案。也许 OP 可以尝试switch 并发布他的解决方案作为答案?

标签: r


【解决方案1】:

两个答案都为您指出了正确的工具,但恕我直言,事情应该这样写。到目前为止,OP 和两种解决方案都在创建使用全局变量 (net) 的函数,这不是最佳实践。

假设randomX 是一个参数net 的函数,即:

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

那你需要做的:

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

或更好:

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

在这两种情况下,输出都是以net 作为输入的函数(就像randomX),因此您可以通过以下方式对其进行评估:

FUN(net)

另请注意,您可以使用第二种方法一次性完成所有操作:

FUN.list[[condition]](net)

【讨论】:

    【解决方案2】:

    另一种解决方案是将要调用的所有函数打包成一个列表randoms,然后根据condition选择一个列表项:

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

    【讨论】:

    • +1 - 就是这个想法,但我认为应该是FUN &lt;- randoms[[condition]] 来获得正确的功能,然后FUN(net) 来评估它。 (在这方面,OP 的代码也搞砸了。)
    【解决方案3】:

    使用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"
    

    更多细节在?switch

    根据您的示例:

    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)
    }
    

    这样就可以了

    这对我很有效:

    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
    

    【讨论】:

    • 这很好,感谢您的帮助,但请参阅我上面的编辑,我应该输入什么而不是 '1' 以使其适用于上述情况?
    • 我收到一条错误消息:分配的左侧无效(do_set)
    • 这正是我所做的,但我收到了我之前评论中提到的错误消息
    • 也:错误:意外'=' in:" switch(condition, 1 ="
    • 如果你想让FUN 成为一个函数,那么switch 应该返回random1 而不是random1(net)。然后你可以通过FUN(net)来评估FUN
    猜你喜欢
    • 2020-09-18
    • 2014-09-06
    • 1970-01-01
    • 2015-10-24
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    相关资源
    最近更新 更多