Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8345413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:39:53+00:00 2026-06-09T06:39:53+00:00

I wrote the following code and got the error: number of items to replace

  • 0

I wrote the following code and got the error: number of items to replace is not a multiple of replacement length at code line:

 X_after[count, ] = c(censN1, censN2, censN3)

After searching the internet, I found the problem is probably caused by the unmatched number of samples size of the pre-determine n_samples of NA and the final X_after dataset. How can I adjust the matrix code such that ncol is dynamically determined after the loop rather than pre-determined at n_samples? Or if you have other solutions to this error message, please chime in as well.

multiLodSim <- function (GM, GSD, n_samples, n_iterations, p) {    
  X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
  delta <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
  mu <- log(GM)
  sigma <- log(GSD)
  lod1 <- quantile(rlnorm(100000,mu,sigma),p)
  lod2 <-  quantile(rlnorm(100000,mu,sigma),(p*0.95))
  lod3 <- quantile(rlnorm(100000,mu,sigma),(p*0.9)) 
  pct_cens <- numeric(n_iterations)
  count <- 1
   while(count <= n_iterations) {     
   sub_samples = n_samples/3   # divide the total sample into third (for 3 lods)
  n1 <- rlnorm(sub_samples,mu,sigma)
censN1 <- sort(pmax(n1,lod1))   
n2 <- rlnorm(sub_samples,mu,sigma)
censN2 <- sort(pmax(n2,lod1))
censN2[censN2==lod1] <- lod2   
n3 <- rlnorm(sub_samples,mu,sigma)
censN3 <- sort(pmax(n3,lod1))    
censN3 [censN3==lod1] <- lod3
X_after[count, ] = c(censN1, censN2, censN3)
delta [count, ] = X_after <= lod1  # nondetects= TRUE (1), detects= FALSE (0)
pct_cens [count] = mean(delta[count,])   #
if (pct_cens [count]  > 0 & pct_cens [count] < 1 ) count <- count + 1}}

 a = multiLodSim(GM=1,GSD=2,n_samples=20,n_iterations=5,p=0.3)

Updates: After reading your comments, I made changes to these code lines and it is working. Thank you for your help.

n1 = rlnorm(round(sub_samples),mu,sigma)
n2 = rlnorm(round(sub_samples),mu,sigma)
sub_samples3 = n_samples - length(n1)-length(n2)
n3 = rlnorm(subsamples3, mu,sigma)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:39:54+00:00Added an answer on June 9, 2026 at 6:39 am

    Your problem lies in the fact that
    sub_samples = n_samples/3 is not a whole number.

    When you create a sample of fractional size it creates a sample of floor(size)

    length(rlnorm(1.5,1,1)) 
    ## [1] 1
    

    Thus, when you recombine your data
    length( c(censN1, censN2, censN3)) does not (necessarily) equal n_sample.

    Thus, you need a method for dealing with numbers of samples that are not divisible by 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote following code...but i am getting Error like: Error 1 'LoginDLL.Class1.Login(string, string, string)':
if wrote the following code and do not understand why the trace returns false:
I wrote the following code to count the node of an XML file: private
I've got some old command-line argument parsing code I wrote 4 years ago for
i wrote following code to create a linkbutton programmatically, but its showing like lable
I wrote the following code for text file encryption and decryption. The encryption process
I wrote the following code to select text from database,but when i echo the
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
I wrote the following code snippet in VS2010: #pragma once #include stdafx.h #ifndef SECURITY_WIN32
I wrote the following code to rotate a cube on x axis an entire

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.