Colleagues After reading in some clinical data, I discovered that the subject ID column contains entries only for the first record for each individual; subsequent rows are recorded as NA. For example:> 1 > NA > NA > NA > NA > 2 > NA > NA > NA > NA > 3 > NA > NA > ...I can think of various approaches to replace the NA values with appropriate entries. I could loop through each row - if the value is NA, I replace it with the entry from the row above. Or, I could find the positions of the non-NA values with match, then replace groups of entries (e.g., positions 2-5) with appropriate entries, again with a loop. But, I expect that R allows some more clever approach to the problem. Any thoughts? Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com [[alternative HTML version deleted]]
The zoo package has na.locf (last occurrernce carried foward) for this purpose:> x[1] 1 NA NA NA NA 2 NA NA NA NA 3 NA NA> library(zoo) > na.locf(x)[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 On 10/21/06, Dennis Fisher <fisher at plessthan.com> wrote:> Colleagues > > After reading in some clinical data, I discovered that the subject ID > column contains entries only for the first record for each > individual; subsequent rows are recorded as NA. For example: > > 1 > > NA > > NA > > NA > > NA > > 2 > > NA > > NA > > NA > > NA > > 3 > > NA > > NA > > ... > > I can think of various approaches to replace the NA values with > appropriate entries. I could loop through each row - if the value is > NA, I replace it with the entry from the row above. Or, I could find > the positions of the non-NA values with match, then replace groups of > entries (e.g., positions 2-5) with appropriate entries, again with a > loop. > > But, I expect that R allows some more clever approach to the > problem. Any thoughts? > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Actually I think its last _observation_ carried forward. On 10/21/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> The zoo package has na.locf (last occurrernce carried foward) > for this purpose: > > > x > [1] 1 NA NA NA NA 2 NA NA NA NA 3 NA NA > > library(zoo) > > na.locf(x) > [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 > > On 10/21/06, Dennis Fisher <fisher at plessthan.com> wrote: > > Colleagues > > > > After reading in some clinical data, I discovered that the subject ID > > column contains entries only for the first record for each > > individual; subsequent rows are recorded as NA. For example: > > > 1 > > > NA > > > NA > > > NA > > > NA > > > 2 > > > NA > > > NA > > > NA > > > NA > > > 3 > > > NA > > > NA > > > ... > > > > I can think of various approaches to replace the NA values with > > appropriate entries. I could loop through each row - if the value is > > NA, I replace it with the entry from the row above. Or, I could find > > the positions of the non-NA values with match, then replace groups of > > entries (e.g., positions 2-5) with appropriate entries, again with a > > loop. > > > > But, I expect that R allows some more clever approach to the > > problem. Any thoughts? > > > > Dennis > > > > Dennis Fisher MD > > P < (The "P Less Than" Company) > > Phone: 1-866-PLessThan (1-866-753-7784) > > Fax: 1-415-564-2220 > > www.PLessThan.com > > > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > >