【问题标题】:number of items to replace is not a multiple of replacement length warning is my function要替换的项目数不是替换长度的倍数警告是我的功能
【发布时间】:2018-02-07 17:19:49
【问题描述】:

首先,我有你的向量,

(x <- c(7.2, 8.6, 9.1, 9.5, 10.9, 10.9, 11.1, 11.5, 11.7,
        11.9, 11.9, 12.7, 12.9, 13.9, 14.1, 14.5, 14.7))
#>  [1]  7.2  8.6  9.1  9.5 10.9 10.9 11.1 11.5 11.7 11.9 11.9 12.7 12.9 13.9 14.1 14.5
#> [17] 14.7
(qjx <- c(6.12, 7.90, 8.85, 9.30, 10.20, 10.90, 11.00, 11.30, 11.60,
          11.80, 11.90, 12.30, 12.80, 13.40, 14.00, 14.30, 14.60, 17.42))
#>  [1]  6.12  7.90  8.85  9.30 10.20 10.90 11.00 11.30 11.60 11.80 11.90 12.30 12.80
#> [14] 13.40 14.00 14.30 14.60 17.42

(rjx <- c(1.78, 0.95, 0.45, 0.9, 0.7, 0.1, 0.3, 0.3,
          0.2, 0.1, 0.4, 0.5, 0.6, 0.6, 0.3, 0.3, 2.82))
#>  [1] 1.78 0.95 0.45 0.90 0.70 0.10 0.30 0.30 0.20 0.10 0.40 0.50 0.60 0.60 0.30 0.30
#> [17] 2.82

x.seqx 的平滑向量,Fm 是确定向量 x 的 c.d.f 值的函数

x.seq <- seq(qjx[1],qjx[m+1],by=0.01)

Fm <- function(data,x){ #data = smooth data, x = intial data
x.smooth <- NULL;
xsmooth <- matrix(,nrow=(length(x)),ncol=length(data))
xsmooth. <- matrix(,nrow=(length(x)),ncol=length(data))
for(i in 1:m){
 for(j in 1:length(data)){
  xsmooth[i,j] <- (data[j] - qjx[i])/rjx[i]
    if(xsmooth[i,j] < 0){
     xsmooth.[i,j] <-  0
    } else
    if(xsmooth[i,j] >= 0 && xsmooth[i,j] <= 1){
     xsmooth.[i,j] = xsmooth[i,j]
    } else
    if(xsmooth[i,j] > 1){
     xsmooth.[i,j] <- 1
    }
 }
}
xsmoothing <- NULL
for(k in 1:ncol(xsmooth.)){
    xsmoothing[k] <- mean(xsmooth.[,k])
}
    print(as.numeric(xsmoothing))
}

接下来我想用下面的函数从函数Fm 计算t 的逆值

m <- length(x)    
Finv <- function(t){
    if(t > 0){
    f.inv <- matrix(,ncol=2,nrow=m)
    k <- 1;f.invers <- NULL
    while(k <= m){
        f.inv[k,1] <- (k-1)/m
        f.inv[k,2] <- k/m
        if(t >= f.inv[k,1] && t <= f.inv[k,2]){
         f.invers[k] <- (((m*t)-(k-1))*rjx[k])+qjx[k]
        } else
         f.invers[k] <- NA
      k <- k+1
      }
     hasil <- as.numeric(f.invers[!is.na(f.invers)])
     print(hasil)
    } else
     hasil <- L # L = the initial value,for example L is 2.06
     print(as.numeric(hasil))
    }

之所以做这个函数,是因为有很多t的序列。所以,我为每个 q 从 1 到最后一个顺序编译 t 的向量

t <- Fm(x.seq,x)
invers <- NULL
for(q in 1:length(t)){
    print(invers[q] <- Finv(1-t[q]))
}
invers

正常工作,但是在处理invers的值时,有警告

Warning messages:
1: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length
2: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length
3: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length
4: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length
5: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length
6: In invers[q] <- Finv(1 - t[q]) :
  number of items to replace is not a multiple of replacement length

我只想知道我的反函数的错误在哪里。谢谢

【问题讨论】:

  • invers[q] &lt;- Finv(1 - t[q]) 中发生了两件事:您正在调用函数 Finv 并将结果分配给列表 invers[q] &lt;-。问题是 R 没有构建这样的列表,请参阅 this question for example
  • @JesseTweedle 我试图将列表中的结果更改为invers[[q]] &lt;- Finv(1 - t[q]) 及其工作,谢谢。但在分析中,我需要一个向量来表示结果。怎么改成向量?
  • 请看下面的答案。如果你想要一个向量(但你的函数需要向量化),通常 R 喜欢一次将函数应用于整个向量。如果您没有矢量化函数,另一种解决方案是使用purrr::map,例如purrr::map(1 - t, Finv) 或其他东西。
  • 抱歉,Finv 函数的某处也有问题。很难找到,但Finv(1 - t[179]) 返回[1] 14.6 14.6!两件事!
  • @JesseTweedle 是的,这也是我关心的问题。但最后我使用invers[[q]] 并使用c(do.call("cbind",invers)) 然后使用unique() 擦除双数据。所以我的逆向量与t的长度相同。

标签: r warnings


【解决方案1】:

在 t 之后试试这个,

invers<-vector()
invers <- NULL
for(q in 1:(length(t)-1)){
  invers<-c(invers,Finv(1-t[q]))
}
invers

这将是我测试的:

 invers <- NULL
 for(q in 1:(length(t))){
   invers<-c(invers,Finv(1-t[q]))
 }
 invers
 invers1<-vector()
 invers1 <- NULL
 for(q in 1:(length(t)-1)){
   invers1<-c(invers,Finv(1-t[q]))
 }
 invers1
 which(invers[1:1134]!=invers1[1:1134])

这是控制台的结果。

 > which(invers[1:1134]!=invers1[1:1134])
 integer(0)

【讨论】:

  • 谢谢,它有效。但是你能解释一下为什么在没有invers[q] 的循环过程中使用invers &lt;- c(invers,Finv(1-t[q])) 吗?
  • invers 是一个空向量。所以声明invers &lt;- c(invers[q], Finv(1-t[q] 暗示了向量的长度。我是 R 新手,这就是我被教导使用 vector() 的方式。来自?vector() -- vector(mode = "logical", length = 0),因此要获取您的信息,模式是 vector() 的名称,长度是返回值的长度。希望对您有所帮助。
猜你喜欢
  • 2020-02-02
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多