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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:41:05+00:00 2026-06-07T17:41:05+00:00

I am developing a censored dependent variable for use in survival analysis. My goal

  • 0

I am developing a censored dependent variable for use in survival analysis. My goal is to find the last time (“time”) that someone answers a question in a survey (e.g. the point where “q.time” is coded as “1”, and “q.time+1” and q at all subsequent times are coded as “0”).

By this logic, the last question answered should be coded as “1” (q.time). The first question that is NOT answered (q.time+1) should be coded as “0”. And all questions subsequent to the first question NOT answered should be coded as “NA”. I then want to remove ALL rows where the DV=NA from my dataset.

A very generous coworker has helped me to develop the following code, but he’s on vacation now and it needs a little more lovin’. Code is as follows:

library(plyr)  # for ddply 
library(stats)  # for reshape(...) 
# From above 
dat <- data.frame( 
  id=c(1, 2, 3, 4), 
  q.1=c(1, 1, 0, 0), 
  q.2=c(1, 0, 1, 0), 
  dv.1=c(1, 1, 1, 1), 
  dv.2=c(1, 1, 0, 1)) 
# From above 
  long <- reshape(dat, 
                direction='long', 
                varying=c('q.1', 'q.2', 'dv.1', 'dv.2')) 
   ddply(long, .(id), function(df) { 
# figure out the dropoff time 
answered <- subset(df, q == 1) 
last.q = max(answered$time) 
subs <- subset(df, time <= last.q + 1) 
# set all the dv as desired 
new.dv <- rep(last.q,1) 
if (last.q < max(df$time)) new.dv <- c(0,last.q) 
subs$dv <- new.dv 
subs 
})

Unfortunately, this yields the error message:

"Error in `$<-.data.frame`(`*tmp*`, "dv", value = c(0, -Inf)) : 
 replacement has 2 rows, data has 0"

Any ideas? The problem seems to be located in the “rep” command, but I’m a newbie to R. Thank you so much!

UPDATE: SEE EXPLANATIONS BELOW, and then REFER TO FOLLOW-UP QUESTION

Hi there-I completely followed you, and really appreciate the time you took to help me out. I went back into my data and coded in a dummy Q where all respondents have a value of “1” – but, discovered where the error really may be. In my real data set, I have 30 questions (i.e., 30 times in long form). After I altered the dataset so FOR SURE q==1 for all id variables, the error message changed to saying

"Error in `$<-.data.frame`(`*tmp*`, "newvar", value = c(0, 29)) : replacement has 2 rows, data has 31"

If the problem is with the number of rows assigned to subs, then is the source of the error coming from…

subs <- subset(df, time <= last.q + 1) 

i.e., $time <= last.q + 1$ is setting the number of rows to the value EQUAL to last.q+1?

UPDATE 2: What, ideally, I’d like my new variable to look like!

 id  time q  dv   
 1    1   1   1
 1    2   1   1
 1    3   1   1
 1    4   1   1
 1    5   0   0
 1    6   0   NA
 2    1   1   1
 2    2   1   1
 2    3   0   0
 2    4   0   NA
 2    5   0   NA
 2    6   0   NA

Please note that “q” can vary between “0” or “1” over time (See the observation for id=1 at time=2), but due to the nature of survival analysis, “dv” cannot. What I need to do is create a variable that finds the LAST time that “q” changes between “1” and “0”, and then is censored accordingly. After Step 4, my data should look like this:

 id  time q  dv   
 1    1   1   1
 1    2   1   1
 1    3   1   1
 1    4   1   1
 2    1   1   1
 2    2   1   1
 2    3   0   0
  • 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-07T17:41:07+00:00Added an answer on June 7, 2026 at 5:41 pm

    First, to give credit where credit is due, the code below is not mine. It was generated in collaboration with another very generous coworker (and engineer) who helped me work through my problem (for hours!).

    I thought that other analysts tasked with constructing a censored variable from survey data might find this code useful, so I am passing the solution along.

    library(plyr)
    #A function that only selects cases before the last time "q" was coded as "1"
    slicedf <- function(df.orig, df=NULL) {
    if (is.null(df)) {
        return(slicedf(df.orig, df.orig))
    }
    if (nrow(df) == 0) {
        return(df)
    }
    target <- tail(df, n=1)
       #print(df)
       #print('--------')
       if (target$q == 0) {
           return(slicedf(df.orig, df[1:nrow(df) - 1, ]))
       }
    if (nrow(df.orig) == nrow(df)) {
        return(df.orig)
    }
    return(df.orig[1:(nrow(df) + 1), ])
    }
    #Applies function to the dataset, and codes over any "0's" before the last "1" as "1"
    long <- ddply(long, .(id), function(df) {
    df <- slicedf(df)
    if(nrow(df) == 0) {
    return(df)
    }
    q <- df$q
    if (tail(q, n=1) == 1) {
    df$q <- rep(1, length(q))
    } else {
    df$q <- c(rep(1, length(q) - 1), 0)
    }
    return(df)
    })
    

    Thanks to everyone online who commented for your patience and help.

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

Sidebar

Related Questions

Developing for Android 2.3, I have a question regarding layouts. I use a vertival
Developing an app that people pay a monthly subscription to use. They can pay
Developing an app that uses data synchronization. Sending images (even resized) takes time if
Iam developing one application.In that iam placing the radio buttons(uiimageview) on table view and
iam developing one application.In that i need to get the music files from the
Developing rails has become lots of fun over the year that I've done it,
Developing for iPhone, I have a collection of points that I need to make
Developing with my first Mac and I noticed that my Rspec output isn't colorized
Developing a network application, I have a Connection class that manages sending and receiving
Developing an iPad website I tried to use the CSS property overflow: auto 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.