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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:09:03+00:00 2026-05-20T08:09:03+00:00

I am creating some artificial data. I need to create household ID (H_ID) and

  • 0

I am creating some artificial data. I need to create household ID (H_ID) and personal ID (P_ID, in each household).

I found a way how to create H_ID in vectorized way.

N <- 50

### Household ID
# loop-for
set.seed(20110224)
H_ID <- vector("integer", N)
H_ID[1] <- 1
for (i in 2:N) if (runif(1) < .5) H_ID[i] <- H_ID[i-1]+1 else H_ID[i] <- H_ID[i-1]
print(H_ID)

# vectorised form
set.seed(20110224)
r <- c(0, runif(N-1))
H_ID <- cumsum(r < .5)
print(H_ID)

But I can not figure out how to create P_ID in vectorized way.

### Person ID
# loop-for
P_ID <- vector("integer", N)
P_ID[1] <- 1
for (i in 2:N) if (H_ID[i] > H_ID[i-1]) P_ID[i] <- 1 else P_ID[i] <- P_ID[i-1]+1
print(cbind(H_ID, P_ID))

# vectorised form
# ???
  • 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-05-20T08:09:03+00:00Added an answer on May 20, 2026 at 8:09 am

    Inspired by Martin Morgan’s solution to a closely related question, here’s a truly vectorized way to generate the P_ID using the cummax function. It becomes clear once you note that P_ID is closely related to the cumsum of !(r < 0.5):

    set.seed(1)
    N <- 10
    r <- c(0, runif(N-1))
    H_ID <- cumsum(r < .5)
    r_ <- r >= .5 # flip the coins that generated H_ID.
    z <- cumsum(r_)  # this is almost P_ID; just need to subtract the right amount...
    # ... and the right amount to subtract is obtained via cummax
    P_ID <- 1 + z - cummax( z * (!r_) )
    > cbind(H_ID, P_ID)
          H_ID P_ID
     [1,]    1    1
     [2,]    1    2
     [3,]    2    1
     [4,]    3    1
     [5,]    3    2
     [6,]    3    3
     [7,]    3    4
     [8,]    4    1
     [9,]    5    1
    [10,]    5    2
    

    I haven’t done detailed timing tests, but it’s probably wicked fast, since these are all internal, vectorized functions

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

Sidebar

Related Questions

I'm creating some text boxes on my form programmatically which I need to reference
I'm creating some big files (DB exports) with Java and I need to put
I am creating some sessions on successful login and I need to access them
I'm interested in creating some web apps that simulate results based on data. For
I'm creating some UIButton s programmatically in a loop but I'm having some problem
I'm creating some custom work items in TFS and the helptext field seems handy
I am creating some build scripts that interact with Perforce and I would like
I am considering creating some JSP-tags that will always give the same output. For
I'm creating some videos from a collection of images, I subsequently wish to play
I'm just getting into creating some WCF services, but I have a requirement to

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.