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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:09:16+00:00 2026-06-17T09:09:16+00:00

Example Here is some data for individual with id = 1 : id time

  • 0

Example

Here is some data for individual with id = 1:

id time status
--------------
1  t    status

t is the time to some event, and status is either 1 if then event occurred or 0 if it did not occurred (in which case t is the duration of the study).

Say that t lies between a2 and a3.

My goal is to transform my data into the following:

id period start stop status
---------------------------
1  1     0     a1   0      
1  2     a1    a2   0      
1  3     a2    t    status  

The total time of individual 1 is divided into three intervals where there is no event in (0, a1) and (a1, a2)

Question

Can you think of an efficient way to write an R-function that inputs a data set and a vector a=(a1, a2, ..., aK) and that outputs the transformed data set?


EDIT

Part 1
I have been asked a concrete example. Here is one:

    id time status
    --------------
    1  5    1

and a1=1, a2=3, a3=7.

Part 2 I have also been asked to show my attempt. Here it is

> data <- data.frame(id=1, time=5, status=1)
> a <- c(1, 3, 7)
> N <- nrow(data)
> data$period <- ifelse(data$time < a[1], 1,
+                       ifelse(data$time < a[2], 2,
+                              ifelse(data$time < a[3], 3, 4)))
> 
> 
> dataTemp1 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp1) <- names(data)
> dataTemp2 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp2) <- names(data)
> dataTemp3 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp3) <- names(data)
> dataTemp4 <- data.frame(matrix(nrow=N, ncol=ncol(data)))
> names(dataTemp4) <- names(data)
> 
> for(j in 1:N)
+ {
+   if(data[j, "period"] == 1){
+     data[j, "start"] <- 0
+     data[j, "stop"] <- data[j, "time"]
+   } else if(data[j, "period"] == 2){
+     dataTemp1[j, c("id", "time", "period")] <-
+       data[j, c("id", "time", "period")]
+     dataTemp1[j, "start"] <- 0
+     dataTemp1[j, "stop"] <- a[1]
+     dataTemp1[j, "status"] <- 0
+     
+     data[j, "start"] <- a[1]
+     data[j, "stop"] <- data[j, "time"] 
+   } else if(data[j, "period"] == 3){
+     dataTemp1[j, c("id", "time", "period")] <-
+       data[j, c("id", "time", "period")]
+     dataTemp1[j, "start"] <- 0
+     dataTemp1[j, "stop"] <- a[1]
+     dataTemp1[j, "status"] <- 0
+     
+     dataTemp2[j, c("id", "time", "period")] <-
+       data[j, c("id", "time", "period")]
+     dataTemp2[j, "start"] <- a[1]
+     dataTemp2[j, "stop"] <- a[2]
+     dataTemp2[j, "status"] <- 0
+     
+     data[j, "start"] <- a[2]
+     data[j, "stop"] <- data[j, "time"]     
+   } else if(data[j, "period"] == 4){
+     dataTemp1[j, c("id", "time", "period")] <-
+       data[j, c("id", "time", "period")]
+     dataTemp1[j, "start"] <- 0
+     dataTemp1[j, "stop"] <- a[1]
+     dataTemp1[j, "status"] <- 0
+     
+     dataTemp2[j, c("id", "time", "period")] <-
+       data[j, c("id", "time", "period")]
+     dataTemp2[j, "start"] <- a[1]
+     dataTemp2[j, "stop"] <- a[2]
+     dataTemp2[j, "status"] <- 0
+     
+     dataTemp3[j, c("id", "time", "period")] <-
+       data[j, c("id",  "time", "period")]
+     dataTemp3[j, "start"] <- a[2]
+     dataTemp3[j, "stop"] <- a[3]
+     dataTemp3[j, "status"] <- 0
+     
+     data[j, "start"] <- a[3]
+     data[j, "stop"] <- data[j, "time"] 
+   }
+ }
> 
> dataTemp1 <- dataTemp1[complete.cases(dataTemp1), ]
> dataTemp2 <- dataTemp2[complete.cases(dataTemp2), ]
> dataTemp3 <- dataTemp3[complete.cases(dataTemp3), ]
> dataTemp4 <- dataTemp4[complete.cases(dataTemp4), ]
> 
> data <- rbind(data, dataTemp1, dataTemp2, dataTemp3, dataTemp4)
> data[, "period"] <- ifelse(data[, "start"] == 0, 1,
+                            ifelse(data[, "start"] == a[1], 2,
+                                   ifelse(data[, "start"] == a[2], 3,
+                                          ifelse(data[, "start"] == a[3], 4,
+                                                 5))))
> data <- data[order(data$id, data$start),
+              c("id", "period", "start", "stop", "status")]
> data
  id period start stop status
2  1      1     0    1      0
3  1      2     1    3      0
1  1      3     3    5      1
  • 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-17T09:09:16+00:00Added an answer on June 17, 2026 at 9:09 am

    I’ll write it as a proper reproducible solution:

    df <- data.frame( id=1, time=5, status=2)
    a <- c(1, 3, 7)
    
    res.fn <- function(df, a) {
        id <- rep(1, length(a))
        period <- 1:length(a)
        start <- c(0, a[1:(length(a)-1)])
        stop <- c(a[1:(length(a)-1)], df$time)
        status <- c(rep(0, length(a)-1), df$status)
        data.frame(id, period, start, stop, status)
    }
    > res.fn(df, a)
    
      id period start stop status
    1  1      1     0    1      0
    2  1      2     1    3      0
    3  1      3     3    5      2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some example data: data = data.frame(series = c(1a, 1b, 1e), reading =
For example (in my case) here's some code, <?php woo_post_inside_before(); if ( $woo_options['woo_post_content'] !=
I have some data as follows (just a few example lines here, tablesize >
Here's some example code: class Obj attr :c, true def == that p '=='
Just some example code here, but I have lists of strings that I want
I am developing a application using PHP. Some example code is here. $url =
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
Here's an example I saw for some GLX code: display = XOpenDisplay(0); // ...
I'm dealing with some gnarly HTML. Here's a stylized example of the ancestry of
I'm working on a stored proc that executes some dynamic sql. Here's the example

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.