r - Data Cleaning for Survival Analysis -


i’m in process of cleaning data survival analysis , trying make individual has single, sustained, transition symptom present (ss=1) symptom remitted (ss=0). individual must have complete sustained remission in order count remission. statistical problems/issues aside, i’m wondering how can go addressing issues detailed below.

i’ve been trying break problem apart smaller, more manageable operations , objects, however, solutions keep coming force me use conditional formatting based on rows above , below missing value and, quite frankly, i’m @ bit of loss how this. love little guidance if think know of technique can use, experiment with, or if know of search terms can use when looking solution.

the details below:

#fake dataset creation id <- c(1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4) time <-c(0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6,0,1,2,3,4,5,6) ss <- c(1,1,1,1,na,0,0,1,1,0,na,0,0,0,1,1,1,1,1,1,na,1,1,0,na,na,0,0) mydat <- data.frame(id, time, ss) 

*bold , underlined characters represent changes dataset above

the goal here find way na values id #1 (variable ss) this: 1,1,1,1,1,0,0

id# 2 (variable ss) this: 1,1,0,0,0,0,0

id #3 (variable ss) this: 1,1,1,1,1,1,na (no change because row na deleted eventually)

id #4 (variable ss) this: 1,1,1,1,1,0,0 (this 1 requires multiple changes , expect challenging tackle).

i don't think have considered "edge case". 2 na's in row @ end of period or 4 or 5 na's in row. give requested solution in tiny test case, however, using na.locf-function:

require(zoo) fillna <- function(vec) { if ( is.na(tail(vec, 1)) ){ vec } else { vec <- na.locf(vec) }                          }  > mydat$locf <- with(mydat, ave(ss, id, fun=fillna)) > mydat    id time ss locf 1   1    0  1    1 2   1    1  1    1 3   1    2  1    1 4   1    3  1    1 5   1    4 na    1 6   1    5  0    0 7   1    6  0    0 8   2    0  1    1 9   2    1  1    1 10  2    2  0    0 11  2    3 na    0 12  2    4  0    0 13  2    5  0    0 14  2    6  0    0 15  3    0  1    1 16  3    1  1    1 17  3    2  1    1 18  3    3  1    1 19  3    4  1    1 20  3    5  1    1 21  3    6 na   na 22  4    0  1    1 23  4    1  1    1 24  4    2  0    0 25  4    3 na    0 26  4    4 na    0 27  4    5  0    0 28  4    6  0    0 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -