Search code examples
rrstudio-server

R error "number of items to replace is not a multiple of replacement


pdf_1 <- vector()

for(i in 1:100) {
  x <- runif(100, 0, 5)
  pdf_1[i] <- x
}

I'm not sure how to get this too run, I keep getting the same error!


Solution

  • I am not sure this is what you want but this way you can fill pdf_1 with values of x:

    pdf_1 <- vector("double", length = 100)
    
    for(i in 1:100) {
      x <- runif(100, 0, 5)
      pdf_1[[i]] <- x[[i]]
    }
    head(pdf_1)
    [1] 4.4140316 2.8390462 2.7328587 0.5423461 0.1792465 2.2133986
    
    

    You need to put an iterator i for x too.