【问题标题】:function to subset n vector giving error message "incorrect number of dimensions"子集 n 向量的函数给出错误消息“维数不正确”
【发布时间】:2021-07-01 18:30:02
【问题描述】:

我正在编写一个函数来从给定向量中的位置的向量中提取一个值。

func_name <- function(input_vec, input_n) {
  sort(input_vec)
  if(input_n > input_vec) {
    return("Warning: input_n exceeds vector length")
  }
  else {
    return(input_vec[,c(input_n)][input_vec == input_n])
    }
}
input_vector <- c(1,2,30,40,50,60)
func_name(input, 3)

我正在尝试获取输出 30,以便函数从向量中提取第三个元素。当我运行该函数时,我收到以下错误消息:

the condition has length > 1 and only the first element will be usedError in input_vec[, c(input_n)] : incorrect number of dimensions

然后我阅读了错误消息,并认为使用 ifelse 语句运行它可能会发生冲突,所以我尝试了以下操作:

find_largestN <- function(input_vec, input_n) {
  sort(input_vec) 
  if(input_n < input_vec) {   
  return(input_vec[,c(input_n)][input_vec == input_n])
  }
  else {
    break
}
}

为此,我收到以下错误消息。

the condition has length > 1 and only the first element will be usedError in find_largestN(input_vec, 1) : 
  no loop for break/next, jumping to top level

我认为这会简化它并减少出错的机会,但显然它不起作用。是否对 if else 语句或函数中的子集有任何建议?

【问题讨论】:

  • 使用ifelse 而不是if/else
  • 我认为您正在寻找函数ifelse,它将向量作为输入,ifelse 只考虑向量的第一个元素
  • 您的条件产生了一个非常简单的错误:您使用input_n &gt; input_vec,它检查input_n 是否超过input_vec 的长度,而是创建一个合乎逻辑的比较向量——比如c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)input_vecc(1, 2, 30, 40, 50, 60)n = 3——因为if一次只能评估一个TRUE/FALSE,它会显示警告the condition has length &gt; 1 and only the first element will be used
  • 正如Skaqqs 的答案所指出的,您需要使用length() 来获得正确的条件:input_n &gt; length(input_vec)
  • 您还犯了一个额外的错误,这一次是在您的 return() 语句中:您应该简单地为 (input_vec[input_n]) 下标,而是将向量 input_vec 视为 @987654347 @:input_vec[,c(input_n)]。自然,将 1 维向量视为 2 维 matrix 会给您错误:Error in input_vec[, c(input_n)] : incorrect number of dimensions

标签: r


【解决方案1】:

先说说ifelseif/else的区别。

ifelse是一种向量化的方法,它以一个向量作为输入,并产生相同长度的向量。

x = rnorm(10)
ifelse(x > 0 , "positive", "negative")
#>  [1] "positive" "negative" "positive" "positive" "negative" "negative"
#>  [7] "positive" "positive" "positive" "negative"

if(x>0) "positive" else negative
#> Warning in if (x > 0) "positive" else negative: the condition has length > 1 and
#> only the first element will be used
#> [1] "positive"

reprex package (v0.3.0) 于 2021-07-02 创建

但是在if/else的情况下,if只需要长度为1的逻辑向量。对于长度大于 1 的向量,第一个元素用于检查条件并引发警告,如上例所示。

查看您的代码,您似乎正在检查if 条件以将向量的长度与提供的索引进行比较。所以应该是:

func_name <- function(input_vec, input_n) {
  if(length(input_vec) < input_n) {
    return("Warning: input_n exceeds vector length")
  }
  else {
    sort(input_vec)
    input_vec[input_n]
  }
}
input_vector <- c(1,2,30,40,50,60)
func_name(input_vector, 3)
#> [1] 30

reprex package (v0.3.0) 于 2021 年 7 月 2 日创建

还请注意,我已将 sort(input_vec) 放在 else 中,因为只有在满足条件时才进行排序是有意义的,一点优化提示。

【讨论】:

    【解决方案2】:

    非常接近!我需要在你的 if 语句中添加 length()

    func_name <- function(input_vec, input_n) {
      sort(input_vec)
      if(input_n > length(input_vec)) {
        return("Warning: input_n exceeds vector length")
      }
      else {
        return(input_vec[input_n])
      }
    }
    
    

    其他人推荐了ifelse()。这可能看起来像

    ifelse(
       test = input_n > length(input_vec),
       yes = print("input_n exceeds vector length"),
       no = input_vec[input_n])
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2020-02-19
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多