Message-ID: <33A1BBE99423A247A16CC54ECE08D865C8B49C@c1bzaek>
Date: 2009-04-06T11:13:58Z
From: Kunzler, Andreas
Subject: time-series data and time-invariant missing values
Dear list,
I have some problems with time-series data and missing values of time-invariant informations like sex or the birth-date.
Assume a data (d) structure like
id birth sex year of observation
1 NA NA 2006
1 1976-01-01 male 2007
1 NA NA 2008
I am looking for a way to replace the missing values.
Right know my answer to this problem slows down R
for (i in 1:length(d[,1])){ # for all observations
if (is.na(d$birth)[i])==F){ # Check if birth of observation(i) is missing
d$birth_2[i] <- as.Date(birth[i],"%d.%m.%Y")
}else{
d$birth2[i] <- d$birth[id[i]==d$id & is.na(d$birth)==F],"%d.%m.%Y")[1] # if birth of observation (i) is missing, take a observation of another year
}
}
}
Result:
id birth sex year of observation birth2
1 NA NA 2006 1976-01-01
1 01.01.1976 male 2007 1976-01-01
1 NA NA 2008 1976-01-01
unfortunately the data consists of over 20000 observations a year.
Does anybody know a better way?
Thanks
Mit freundlichen Gr??en
Andreas Kunzler
____________________________
Bundeszahn?rztekammer (BZ?K)
Chausseestra?e 13
10115 Berlin
Tel.: 030 40005-113
Fax: 030 40005-119
E-Mail: a.kunzler at bzaek.de