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

  • Home
  • SEARCH
  • 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 6815515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:49:01+00:00 2026-05-26T20:49:01+00:00

Context: As far as I can see, R lacks consistent functions which facilitate the

  • 0

Context: As far as I can see, R lacks consistent functions which facilitate the data preparation in the context of survival/event history analysis, e.g. episode-splitting to include time-varying covariates (sometimes refered to as ‘counting process data’).

For each individual (id), the start (start.cp) and end time (stop.cp) of each episode is given. Furthermore, for each of the 1,2, …, p time-varying covariates (TVC), we know when the episode starts (tvc.start_) and when it ends (tvc.stop_).

In my example (see below) the number of TVCs is 2 but usually the number can vary (from 1 to p).

Example:

Input data:

  id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2
1  1        1       2          2          3         4         7
2  1        2       3          2          3         4         7
3  1        3       4          2          3         4         7
4  1        4       7          2          3         4         7
5  1        7      12          2          3         4         7

structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4, 
7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
), tvc.stop2 = c(7, 7, 7, 7, 7)), .Names = c("id", "start.cp", 
"stop.cp", "tvc.start1", "tvc.start2", "tvc.stop1", "tvc.stop2"), 
row.names = c(NA, 5L), class = "data.frame")

The names of the TVCs are known, i.e. in this example it is known that

tvc.start <- c("tvc.start1", "tvc.start2") 
tvc.stop <- c("tvc.stop1", "tvc.stop2")

Expected results:

  id start.cp stop.cp tvc.start1 tvc.start2 tvc.stop1 tvc.stop2 tvc.d1 tvc.d2
1  1        1       2          2          3         4         7      0      0
2  1        2       3          2          3         4         7      1      0
3  1        3       4          2          3         4         7      1      0
4  1        4       7          2          3         4         7      0      1
5  1        7      12          2          3         4         7      0      1

structure(list(id = c(1, 1, 1, 1, 1), start.cp = c(1, 2, 3, 4, 
7), stop.cp = c(2, 3, 4, 7, 12), tvc.start1 = c(2, 2, 2, 2, 2
), tvc.start2 = c(3, 3, 3, 3, 3), tvc.stop1 = c(4, 4, 4, 4, 4
), tvc.stop2 = c(7, 7, 7, 7, 7), tvc.d1 = c(0, 1, 1, 0, 0), tvc.d2 = c(0, 
0, 0, 1, 1)), .Names = c("id", "start.cp", "stop.cp", "tvc.start1", 
"tvc.start2", "tvc.stop1", "tvc.stop2", "tvc.d1", "tvc.d2"), row.names = c(NA, 
5L), class = "data.frame")

Question: For each TVC, I would like to create a new vector (tvc.d1, tvc.d2, see example) which indicates that a given episode (defined by start.cp and stop.cp) overlaps (=1) the interval of a TVC. It is assumed that [start.cp, stop.cp). How can this be done without looping over the set of TVCs, i.e. I am looking for a vectorized solution.

P.S.: Please feel free to change the title…

  • 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-26T20:49:01+00:00Added an answer on May 26, 2026 at 8:49 pm

    I think Terry Therneau might want to dispute your claim, The tcut function and the pyearsin the recommended survival package are described early in his technical article with Cindy Crowson on handling time-dependent covariates. I had trouble understanding why should tcv.d1 be contributing exposure during the interval 2 -> 3 when its stop time was 2? But the explanation for later readers is in the comments to the question.

    You really only need the start.cp stop.cp vectors and the first line as input data. You compare the interval defining vector to the vector of each component/indivdiual’s start and stop vector and find the intervals that == ‘1’s. I’m ondering if the data doesn’t really come in this way and you might not need to do the duplication of start and stop times in your setup.

    tvec <- with(dat, c(start.cp[1], stop.cp))
    dat$tvc.d1 <- 1*( findInterval(tvec,      # the "1*" converts to numeric
                                   as.numeric( dat[ 1, c("tvc.start1", "tvc.stop1")]) ,  
                                   all.inside=FALSE)[1:5] == 1)
    dat$tvc.d2 <- 1*( findInterval(tvec, 
                                   as.numeric( dat[ 1, c("tvc.start2", "tvc.stop2")]) ,  
                                   all.inside=FALSE)[1:5] == 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know I can access the web.xml <context-param> s by making
As far as I can see there are two main principles how to deal
I'm having a weird issue that I can't track down... For context, I have
Can I access the this object in my extension methods? So far this is
You usually normalize a database to avoid data redundancy. It's easy to see in
I am working with enumerations in Java. As I can see, it is possible
I'm trying to redraw the content of a simple loop. So far it prints
Context: I'm in charge of running a service written in .NET. Proprietary application. It
Context: So, I am attempting to build a ridiculously complex domain model. Talking with
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp

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.