【问题标题】:Switch statement is not working for numerical objectsSwitch 语句不适用于数字对象
【发布时间】:2021-12-09 22:47:18
【问题描述】:

我是 R 编程新手。我不知道我们是否可以对数字对象使用 switch 语句。

这是我的代码,

myfunction <- function() {
    x <- 10
    switch(x,
        1={
            print("one")
        },
        2={
            print("two")
        },
        3={
            print("three")
        },
        {
            print("default")     #Edited one..
        }
    )
}

我收到了这个错误,

test.R:4:18: unexpected '='
3:         switch(x,
4:                 1=
                    ^

请帮我解决这个问题。

【问题讨论】:

    标签: r switch-statement


    【解决方案1】:
    myfunction <- function(x) {
                           switch(x,
                                  print("one"),
                                  print("two"),
                                  print("three"))}
    
    myfunction(1)
    ## [1] "one"
    

    编辑: 如 cmets 中所述,此方法不评估正在输入的值,而是将它们用作索引。因此,它适用于您的情况,但如果要重新排序语句,它将不起作用(有关更好的方法,请参阅@Joshs 答案)。

    无论哪种方式,我认为switch 不是在这种情况下使用的正确功能,因为它主要用于在不同的替代方案之间切换,而在您的情况下,您基本上是一遍又一遍地运行相同的功能.因此,为每个备选方案添加额外的语句似乎工作量太大(例如,如果您想显示 20 个不同的数字,则必须编写 20 个不同的语句)。

    相反,您可以尝试english 包,它允许您显示在ifelse 语句中定义的任意数量的数字

    library(english)
    myfunction2 <- function(x) {
                     ifelse(x %in% 1:3, 
                            as.character(as.english(x)), 
                            "default")}
    myfunction2(1)
    ## [1] "one"
    myfunction2(4)
    ## [1] "default"
    

    或者,您也可以通过使用match 来避免使用switch(尽管不一定推荐)

    myfunction3 <- function(x) {
      df <- data.frame(A = 1:3, B = c("one", "two", "three"), stringsAsFactors = FALSE)
             ifelse(x %in% 1:3, 
              df$B[match(x, df$A)],
              "default")}
    myfunction3(1)
    ## [1] "one"
    myfunction3(4)
    ## [1] "default"
    

    【讨论】:

    • 如果你不介意告诉我如何为这个 switch 语句添加默认值?
    • 例如,用function(x = 1) 代替function(x)(如果您希望1 成为默认值)。您也可以删除所有名称,然后像 MrFlick 提到的那样输入 switch(x, {print("one")}, {print("two")}, {print("three")} )(没有名称)
    • 我想我的问题并不清楚。对此感到抱歉。我想为我的 switch 案例提供默认案例,我该怎么做?
    • 就像我说的那样...设置为function(x = 1),然后尝试运行myfunction()。不然我不懂你
    • 来自?switch 帮助页面上的示例:“Numeric EXPR 不允许指定默认值。”返回一个 NULL 值。如果愿意,您可以测试是否返回 NULL,也可以强制进行字符转换。但我不确定这是你想要的行为。见:myfunction &lt;- function(x) {switch(x,3=print("three"),1=print("one"),2=print("two"))}; myfunction(3)
    【解决方案2】:

    我建议阅读?switch 帮助页面。这似乎在那里描述得很好。 R 中的名称永远不能是数字,即不允许使用 c(1=5),也不允许使用 f(1=5, 2=5)。如果你真的有 1,2 或 3,那么你只想要

    switch(x,
        {print("one")},
        {print("two")},
        {print("three")}
    )
    

    (省略数值的名称)

    【讨论】:

      【解决方案3】:

      为了充分利用switch 的功能(特别是它使用最终“默认”表达式处理任意值的能力)处理 1、2、3 以外的数字, ...,您最好将任何输入转换为字符串。

      我会这样做:

      myfunction <- function(x) {
          switch(as.character(x),
              "1" = print("one"),
              "2" = print("two"),
              "3" = print("three"),
              print("something other than 'one', 'two', or 'three'"))
      }
      
      myfunction(1)
      # [1] "one"
      myfunction(345)
      # [1] "something other than 'one', 'two', or 'three'"
      

      【讨论】:

        猜你喜欢
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        相关资源
        最近更新 更多