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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:29:10+00:00 2026-06-15T08:29:10+00:00

I am trying to create a variable called strata for recurrent events. The idea

  • 0

I am trying to create a variable called strata for recurrent events. The idea is to define a variable that counts events, but filling previous records. What defines the counting is the variable event as is shown below:

id event   cov    strata      year
1   0       0        1        12
1   0       1        1        13
1   1       1        1        14
1   0       1        2        15
1   1       0        2        16
1   1       1        3        17
1   0       0        4        18
1   0       1        4        19
1   0       1        4        20

I have tried something like this at least for the event records:

id  <- c(rep(1,9), rep(2,5), rep(3,7))
event  <- c(0,0,1,0,1,1,0,0,0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1)
cov  <- c(0,1,1,1,0,1,0,1,1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1)
year  <- c(seq(12,20,1), seq(12,16,1), seq(12,18,1)) 
dat  <- data.table(id,event, cov, year)

dat[,strata:=seq(.N),by="id"]

But it doesn’t work.

Any ideas?
Thanks

  • 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-15T08:29:12+00:00Added an answer on June 15, 2026 at 8:29 am

    Here is an updated solution.

    dat  <- data.table(id,event, cov, year)
    dat[, strata := c(1L,head(cumsum(event)+1L,-1L)), by = id]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a boost::function that allows setting of a member variable of
I am trying to create a variable that gets the current date and time
I have been trying to create a pointer variable in the called function and
I'm trying to create a Macro that copies variable columns from one sheet to
I'm trying to create a variable background, where the image changes based on the
I am running a loop, and I am trying to create a variable each
I'm trying to create a Vista gadget, I've properly stored my Date1 variable and
I am trying to created nested dictionary variable like the below, But I get
I'm trying to understand if it's possible to create a set of variables that
I've been trying to create a new variable of type Cmplx (which is my

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.