s-news
[Top] [All Lists]

Re: lag down

To: "Charles E. (Ted) Wright" <cew@aris.ss.uci.edu>
Subject: Re: lag down
From: Peng Huang <huangp@musc.edu>
Date: Mon, 16 Jun 2003 13:45:25 -0400
Cc: Bernd Puschner <bernd.puschner@bkh-guenzburg.de>, S news submissions <s-news@wubios.wustl.edu>
References: <Pine.GSO.4.50.0306160751550.12656-100000@aris.ss.uci.edu>
If there are more than two observations per unique id,
we may try the following:

> data
   id  x 
 1  2 33
 2  2 NA
 3  3 67
 4  3 NA
 5  3 NA
 6  3 NA
 7  3 NA
 8  4 24
 9  4 NA
10  4 NA
11  4 NA
12  5 45
13  5 NA
14  5 NA
15  5 NA
16  6 29
17  6 NA
18  6 NA
19  6 NA
20  6 NA

# First sort data by id using sort.col function.
 data<- sort.col(data, c(1,2), ascending=T)
 x1<- rle(data$id)
 x<- data$x[is.na(data$x)==F]
 data$x<- rep(x, times=x1$lengths)

> data
   id  x 
 1  2 33
 2  2 33
 3  3 67
 4  3 67
 5  3 67
 6  3 67
 7  3 67
 8  4 24
 9  4 24
10  4 24
11  4 24
12  5 45
13  5 45
14  5 45
15  5 45
16  6 29
17  6 29
18  6 29
19  6 29
20  6 29

Hope it helps.

Peng Huang, Ph.D.
Assistant Professor
Department of Biometry and Epidemiology
Medical University of South Carolina
135 Cannon Street, Suite 303
Post Office Box 250835
Charleston, SC 29425, USA
Tel: 843-876-1134
Fax: 843-876-1126


Bernd Puschner wrote:
> 
> dear s-plus users,
> 
> can't figure out a solution to a simple problem.
> i have a data frame with one observation (x) per subject (id)
> 
> id      x
> 1       2
> 1       NA
> 2       1
> 2       NA
> 3       2
> 3       NA
> 
> how do i "lag down" the observation per subject so that the data frame looks
> like
> 
> id      x
> 1       2
> 1       2
> 2       1
> 2       1
> 3       2
> 3       2
> 
> etc.
> 
> any help would be appreciated.
> 
> thanks.
> 
> bernd
> 
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>