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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:11:47+00:00 2026-06-17T22:11:47+00:00

I am working on a wide dataset, and now I would like to use

  • 0

I am working on a “wide” dataset, and now I would like to use a specific package (-msSurv-, for non-parametric multistate models) which requires data in interval form.

My current dataset is characterized by one row for each individual:

dat <- read.table(text = "

   id    cohort   t0    s1     t1     s2      t2     s3    t3
    1      2      0      1     50      2      70     4     100
    2      1      0      2     15      3      100    0     0   

", header=TRUE)

where cohort is a time-fixed covariate, and s1–s3 correspond to the values that a time-varying covariate s = 1,2,3,4 takes over time (they are the distinct states visited by the individual over time). Calendar time is defined by t1–t3, and ranges from 0 to 100 for each individual.

So, for instance, individual 1 stays in state = 1 up to calendar time = 50, then he stays in state = 2 up to time = 70, and finally he stays in state = 4 up to time 100.

What I would like to obtain is a dataset in “interval” form, that is:

id   cohort  t.start    t.stop   start.s   end.s          
1      2        0         50        1        2
1      2       50         70        2        4
1      2       70        100        4        4
2      1        0         15        2        3
2      1       15        100        3        3

I hope the example is sufficiently clear, otherwise please let me know and I will try to further clarify.

How would you automatize this reshaping? Consider that I have a relatively large number of (simulated) individuals, around 1 million.

Thank you very much for any help.

  • 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-17T22:11:48+00:00Added an answer on June 17, 2026 at 10:11 pm

    I think I understand. Does this work?

    require(data.table)
    dt <- data.table(dat, key=c("id", "cohort"))
    dt.out <- dt[,  list(t.start=c(t0,t1,t2), t.stop=c(t1,t2,t3), 
                         start.s=c(s1,s2,s3), end.s=c(s2,s3,s3)), 
                         by = c("id", "cohort")]
    
    #    id cohort t.start t.stop start.s end.s
    # 1:  1      2       0     50       1     2
    # 2:  1      2      50     70       2     4
    # 3:  1      2      70    100       4     4
    # 4:  2      1       0     15       2     3
    # 5:  2      1      15    100       3     0
    # 6:  2      1     100      0       0     0
    

    If the output you show is indeed right and is what you require, then you can obtain with two more lines (not the best way probably, but it should nevertheless be fast)

    # remove rows where start.s and end.s are both 0
    dt.out <- dt.out[, .SD[start.s > 0 | end.s > 0], by=1:nrow(dt.out)]
    # replace end.s values with corresponding start.s values where end.s == 0
    # it can be easily done with max(start.s, end.s) because end.s >= start.s ALWAYS
    dt.out <- dt.out[, end.s := max(start.s, end.s), by=1:nrow(dt.out)]
    dt.out[, nrow:=NULL]
    
    > dt.out
    #    id cohort t.start t.stop start.s end.s
    # 1:  1      2       0     50       1     2
    # 2:  1      2      50     70       2     4
    # 3:  1      2      70    100       4     4
    # 4:  2      1       0     15       2     3
    # 5:  2      1      15    100       3     3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a multi-threaded Qt application and would like to connect a signal
Preface I had a system-wide installation of Ipython which was working fine. However, when
(Postgres 8.3) I'm working with a DB table X 100+ columns wide (which I
I'm working with unicode/wide characters and I'm trying to create a toString method (
We're working on a pretty big and wide application. The website will have a
I am working on a website that is 960px wide. It is mainly geared
I am working with a dataset that comes with lme4, and am trying to
The website I'm working on has a wide main content block that doesn't fully
Having a wide choices of gems is one of the strengths of working with
I am working with a rarefaction output from mothur , which basically gives me

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.