【问题标题】:Trouble with R and vector length NA values appended附加 R 和向量长度 NA 值的问题
【发布时间】:2018-09-24 03:05:08
【问题描述】:

我已经看过了,我不太明白为什么这会给我附加到我想要的向量上的 NA 值。提示如下:

"函数应该返回一个向量,其中第一个元素是输入向量的第一个n元素之和,向量的其余部分是输入向量其他元素的副本。例如,如果输入向量是(2, 3, 6, 7, 8)n = 2,那么输出应该是向量(5, 6, 7, 8)"

 testA<- c(1,2,3,4,5)    
myFunction <- function(vector1, n)
       {
       sum1=0
       for(i in 1:n)
         {
          sum1<-sum1+vector1[i]
          newVector<-c(sum1,vector1[n+1:length(vector1)])
         }
       return(newVector)
         }

print(newVector) 
myFunction(testA, 3)

输出是:[1] 6 4 5 NA NA NA,而它应该只是 6 4 5

【问题讨论】:

    标签: r vector


    【解决方案1】:

    这里不需要for 循环;你可以这样做

    test <- c(2, 3, 6, 7, 8)
    
    myfunction <- function(x, n) c(sum(x[1:n]), x[-(1:n)])
    myfunction(test, 2)
    #[1] 5 6 7 8
    
    testA <- c(1,2,3,4,5)   
    myfunction(testA, 3)
    #[1] 6 4 5
    

    解释:sum(x[1:n]) 计算xx[-(1:n)] 的第一个n 元素的总和,在删除第一个n 元素后返回x

    【讨论】:

      【解决方案2】:

      可以通过headtail 完成

      n <- 2
      c(sum(head(test, 2)), tail(test, -2))
      #[1] 5 6 7 8
      

      数据

      test <- c(2, 3, 6, 7, 8)
      

      【讨论】:

        【解决方案3】:

        这里我尝试比较一下上面两个函数的效率,分别是答案帖https://stackoverflow.com/a/52472214/3806250和问题帖。

        > testA <- 1:5    
        > myFunction <- function(vector1, n) {
        +        sum1 <- 0
        +        for(i in 1:n) {
        +           sum1 <- sum1 + vector1[i]
        +           newVector <- c(sum1, vector1[n+1:length(vector1)])
        +          }
        +        newVector <- newVector[!is.na(newVector)]
        +        return(newVector)
        +        }
        > 
        > microbenchmark::microbenchmark(myFunction(testA, 3))
        Unit: microseconds
                         expr   min     lq     mean median    uq     max neval
         myFunction(testA, 3) 3.592 4.1055 77.37798  4.106 4.619 7292.85   100
        > 
        > myfunction <- function(x, n) c(sum(x[1:n]), x[-(1:n)])
        > 
        > microbenchmark::microbenchmark(myfunction(testA, 2))
        Unit: microseconds
                         expr   min   lq     mean median    uq      max neval
         myfunction(testA, 2) 1.539 1.54 47.04373  2.053 2.053 4462.644   100
        

        【讨论】:

          【解决方案4】:

          谢谢大家的回答!昨晚实在是太累了,想不出这么简单的办法:

              function(vector1, n)
            {
            sum1=0
            for(i in 1:n) #scans input vector from first element to input 'n' element
              {
              sum1<-sum1+vector1[i]#Find sum of numbers scanned 
              newVector<-c(sum1,vector1[n+1:length(vector1)])#new output vector starting with the sum found then concatonates rest of the original vector after 'n' element
              length(newVector)<-(length(newVector)-(n)) #NA values were returned, length needs to be changed with respect to 'n'
            }
            return(newVector)
            print(newVector)
          }
          

          【讨论】:

            【解决方案5】:

            已经有很好的解决方案,但这里有另一个选项可以对您的原始代码进行最少的修改:

            testA<- c(1,2,3,4,5)    
            myFunction <- function(vector1, n)
            {
              sum1=0
              for(i in 1:n)
              {
                sum1<-sum1+vector1[i]
              }
            
              newVector<-c(sum1,vector1[(n+1):length(vector1)]) # we take this line out of the for loop
              # and put the n+1 in between parenthesis
            
              return(newVector)
            }
            newVector <- myFunction(testA, 3)
            print(newVector) 
            

            原始代码/示例的问题是 n+1:length(vector1) 应该返回 [1] 4 5,以便进行适当的子集化(获取向量中未包含在第一个元素之和中的最后一个元素n 元素),但它实际上返回了 [1] 4 5 6 7 8。由于testA中的6:8位置没有元素,这就是出现缺失值/NA的原因。

            n+1:length(vector1) 实际上在做的是首先获得安全1:length(vector1),然后将n 添加到每个元素。下面是一个使用值的行为示例:

            3+1:5
            #> [1] 4 5 6 7 8
            

            我们可以通过将n+1 放在原始代码的括号之间来解决这个问题。在我们使用值的示例中:

            (3+1):5
            #> [1] 4 5
            

            此外,将newVector 的赋值从循环中取出可以提高性能,因为sum1 和子集向量之间的绑定只需要在第一个n 元素的总和完成后完成。

            【讨论】:

              猜你喜欢
              • 2020-01-30
              • 2017-01-08
              • 1970-01-01
              • 2017-08-31
              • 1970-01-01
              • 2014-08-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多