【问题标题】:If...else error 'the condition has lenght >1...' with multiple operations at each statementIf...else error 'the condition has lenght >1...' 在每个语句中都有多个操作
【发布时间】:2023-03-10 13:04:01
【问题描述】:

我在一个函数中有一系列if 语句。它看起来像这样:

my_func <- function(data, selection) {

 if (selection == 'p+c') {
 predictors = 'chicago'
 preds <- data
}
else if (selection== 'p') { 
  predictors = 'new_york'
  preds <- data %>% dplyr::select(-c(region, sale))
}
else if (selection == 'c') {
  predictors = 'california'
  preds <- data %>% dplyr::select(region, sale)
} 
# then the function does something else with predictors and preds, 
#  and returns a dataframe  
}

my_func(my_data, selection = 'p')

我不断收到the condition has length &gt; 1 and only the first element will be used 的警告。奇怪的是,它实际上并没有破坏任何东西(一切都按预期工作),但我仍然宁愿修改这个问题。

我读到这是矢量化的问题,但我不知道如何解决这个问题。

我已经尝试将 if/else 替换为 ifelse(如其他帖子中所建议的那样),但这不起作用,可能是因为我在每个 if 语句中执行了多个操作。我这样做了:

 ifelse (selection == 'p+c') {
 predictors = 'chicago'
 preds <- data
}
ifelse (selection== 'p') { 
  predictors = 'new_york'
  preds <- data %>% dplyr::select(-c(region, sale))
}
ifelse (selection == 'c') {
  predictors = 'california'
  preds <- data %>% dplyr::select(region, sale)
}

【问题讨论】:

  • “我已经尝试用 ifelse 替换 if/else(如其他帖子中所建议的那样),但这不起作用”——显示您尝试过的内容。对ifelse 进行嵌套调用是没有问题的。
  • 您根本没有将ifelse 用作函数。如果您熟悉 Excel,ifelse 的作用类似于 Excel 的 IF 函数。
  • 这意味着selection 中包含多个值,例如c('p+c', 'p')。在这种情况下会输出什么?
  • selection 包含什么内容?
  • 您的代码对我来说仍然无法重现或清晰。所以selection 可以有 3 个值,但在这种情况下预期的输出是什么?考虑添加一个示例虚拟数据帧并向我们展示它的预期输出。阅读how to give a reproducible example

标签: r


【解决方案1】:

ifelse 是一个函数,您需要将它的结果分配给您的变量(而不是将分配放在函数调用本身中)。如果没有可重现的示例(您忽略了提供 - 请参阅 How to make a great R reproducible example?),很难确定以下代码完全符合您的要求,但类似于:

predictors <- ifelse(selection == 'p+c',
    'chicago',
     ifelse(selection == 'p',
         'new york',
          ifelse(selection == 'c',
              'california',
              'NA')))

(preds 的代码类似)。

【讨论】:

  • 谢谢。那么我是否必须运行两次,一次使用名称(例如“chicago”),一次更改数据(例如preds)?
【解决方案2】:

您有两个问题。留言

条件的长度 > 1...

出现是因为if() 未矢量化。我假设 selection 有多个值。

ifelse 在您只有两个选项时最有用。对于多个选项,一个不错的选项是嵌套else if() 语句:

没有数据,我无法检查这个,嵌套的else if 解决方案是:

 if (selection == 'p+c') {
   predictors = 'chicago'
   preds <- data
 } else if(selection == 'p') { 
   predictors = 'new_york'
   preds <- data %>% dplyr::select(-c(region, sale))
 } else if (selection == 'c') {
   predictors = 'california'
   preds <- data %>% dplyr::select(region, sale)
 } else { # Good practice to capture errors safely
   stop("Selection not found")
 }

如果您需要将 selection 保留为向量,例如selection &lt;- c("p+c", "c") 然后你把上面的语句变成一个函数并将它传递给一个'apply()'语句,例如

checkFunction <- function(selection) {
    if(selection == 'p+c') {
      predictors = 'chicago'
      preds <- data
    } else if(selection == 'p') { 
      predictors = 'new_york'
      preds <- data %>% dplyr::select(-c(region, sale))
    } else if (selection == 'c') {
      predictors = 'california'
      preds <- data %>% dplyr::select(region, sale)
    } else { # Good practice to capture errors safely
      stop("Selection not found")
    }
  
  return(list(predictors, preds))
}
   
output <- sapply(selection, checkFunction)

 > output
     p+c       c        
[1,] "chicago" "california
[2,] tbl_df,3  tbl_df,2 

output[,1]

[[1]]
[1] "chicago"

[[2]]
# A tibble: 5 x 3
  region  sale other
   <int> <int> <int>
1      1     2     3
2      2     3     4
3      3     4     5
4      4     5     6
5      5     6     7

【讨论】:

    猜你喜欢
    • 2023-01-16
    • 1970-01-01
    • 2017-10-29
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    相关资源
    最近更新 更多