为什么我的sapply函数构建整数向量?

时间:2013-01-19 20:27:26

标签: r vector sapply

我有两个相关的问题 - 我正在努力学习R,所以我正在从R课程做一些功课问题。他们让我们编写一个函数来返回相关向量:

example.function <- function(threshold = 0) {
  example.vector <- vector()
  example.vector <- sapply(1:30, function(i) {
    complete.record.count <- # ... counts the complete records in each of the 30 files.
    ## Cutting for space and to avoid giving away answers.
    ## a few lines get the complete records in each 
    ## file and count them. 
    if(complete.record.count > threshold) {
      new.correlation <- cor(complete.record$val1, complete.record$val2)
      print(new.correlation)
      example.vector <- c(new.correlation, example.vector)
    }  
  })
  # more null value handling#
  return(example.vector)
}

当函数运行时,它会将相关值打印到stdout。它打印的值精确到六个小数点。所以我知道我为new.correlation.得到了一个很好的值。返回的向量不包括这些值。相反,它是整数。

> tmp <- example.function()
> head(tmp)
[1] 2 3 4 5 6 7

我无法弄清楚为什么sapply将整数推入向量?我在这里缺少什么?

我实际上并不了解核心结构,或多或少:

some.vector <- vector()
some.vector <- sapply(range, function(i) {
  some.vector <- c(new.value,some.vector)
}

在冗余方面似乎非常不像R一样。提示?

1 个答案:

答案 0 :(得分:1)

如果你使用sapply,你不需要自己创建矢量,也不需要增长它(sapply负责所有这些)。你可能想要这样的东西:

example.function <- function(threshold = 0) {
  example.vector <- sapply(1:30, function(i) {
    ## Cutting for space and to avoid giving away answers.
    ## a few lines get the complete records in each 
    ## file and count them. 
    if(complete.record.count > threshold) {
      new.correlation <- cor(complete.record$val1, complete.record$val2)
      }  else {
        new.correlation <- NA   
      }
    new.correlation #return value of anonymous function
  })
  # more null value handling#
  example.vector #return value of example.function
}

但是,目前还不清楚索引i是如何影响匿名函数的,而且问题是不可重现的......