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 9230681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:53:03+00:00 2026-06-18T05:53:03+00:00

I experience some problems with the loop function in R and couldn’t find an

  • 0

I experience some problems with the loop function in R and couldn’t find an answer on this question on this website. I would like to use a numeric vector as input for a loop in R.

For example:

ns <- c(10, 20, 40, 80, 160)

for (n in ns) {
 ni[n] <- round(rnorm(1, mean = n, sd = 1))
}

The result of this code is a vector with 155 times NA and five correct values in this vector. However, I would like to get rid of all those NAs and get a vector with only the five correct values. I know how to select the correct values from the vector with the 155 NA, but I prefer to obtain a proper vector directly after running the loop.

Thank you in advance!

  • 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-18T05:53:04+00:00Added an answer on June 18, 2026 at 5:53 am

    Remember that many functions in R are vectorized

    > rnorm(length(ns), mean=ns)
    [1]   9.905652  19.721717  40.462751  78.982971 160.770257
    

    (in your question, ni[n] creates a vector as long as the maximum of n, i.e., 160 elements).

    It’s interesting to see this evolve from an sapply solution offered by @VictorK.

    sapply(ns, function(n) round(rnorm(1, mean = n, sd = 1)))
    

    factor out the round and drop the default argument sd = 1, so

    round(sapply(ns, function(n) rnorm(1, mean = n)))
    

    then recognize that rnorm could replace the anonymous function function(n) ... if we name it’s first argument in the sapply call. The first argument of rnorm is named n, so things are a little confusing; but we’re forcing the elements of ns to match the second argument mean. For instance the first time through the sapply we evaluate rnorm(ns[[1]], n=1). R matches arguments first by name, so n=1 matches the first argument of rnorm, and then by position amongst the remaining arguments, so the unnamed argument ns[[1]] matches the next available argument, mean)

    round(sapply(ns, rnorm, n = 1))
    

    and then perhaps we see the fully vectorized solution

    round(rnorm(n = length(ns), mean = ns))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I experience some trouble trying to create a function that contains a loop in
After some experience with functional languages, I'm starting to use recursion more in Java
I am using a .dll from Ingeardrivers.com. I realize this question would more appropriately
I've got some problems/misunderstandings with arrays in C++. int myArray[30]; myArray[1]=2; myArray[2]=4; This is
we currently experience some file writing/encoding problems that we cannot reproduce. Actually there are
all. We're trying to get some intersect collisions working, but the problem experience is
I had some experience of android but know almost nothing of GCC Makefile. Here
I have some experience using parallel extensions in .Net development, but I was looking
I have some experience in ASP.Net and can work my way around it without
I have some experience writing web applications in Java for class projects. My first

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.