There are lots of vector-based ways to do this too. This is a quick vector
based alternative ...
> # Create vector
> myText <- c("Johnson", "", "", "", "", "Smith", "", "")
> # Fill in blanks
> isBlank <- myText == ""
> rep(myText[!isBlank], table(cumsum(!isBlank)))
[1] "Johnson" "Johnson" "Johnson" "Johnson" "Johnson" "Smith" "Smith"
"Smith"
This works if no leading blanks ... needs an additional line if there are
...
Cheers,
Rich.
mangosolutions
S-PLUS & R Training and Consulting
Tel +44 118 902 6617
Fax +44 118 902 6401
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Chuck Cleland
Sent: 01 February 2006 17:39
To: CHUL LEE
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] replace missing with previous value
> X <- sample(c("Smith", "", "Johnson"), 80, replace=TRUE)
> X
[1] "Johnson" "" "" "Johnson" "Smith"
[6] "" "" "Smith" "Johnson" "Johnson"
[11] "" "" "Smith" "Smith" "Johnson"
[16] "Johnson" "" "" "Smith" "Johnson"
[21] "Smith" "Johnson" "Smith" "" "Smith"
[26] "Smith" "" "Smith" "Johnson" ""
[31] "" "Smith" "" "" "Johnson"
[36] "Smith" "Smith" "" "Johnson" ""
[41] "Smith" "Smith" "Smith" "Johnson" ""
[46] "" "" "" "" "Johnson"
[51] "" "" "" "Johnson" "Johnson"
[56] "Johnson" "" "Johnson" "Johnson" "Smith"
[61] "" "" "" "Johnson" "Johnson"
[66] "" "" "Smith" "Smith" "Johnson"
[71] "" "Johnson" "" "Johnson" "Johnson"
[76] "Smith" "Smith" "Johnson" "Smith" "Smith"
> for(i in seq(along=X)[-1]){X[i] <- if(X[i] == "" | is.na(X[i])) X[i -
1] else X[i]}
> X
[1] "Johnson" "Johnson" "Johnson" "Johnson" "Smith"
[6] "Smith" "Smith" "Smith" "Johnson" "Johnson"
[11] "Johnson" "Johnson" "Smith" "Smith" "Johnson"
[16] "Johnson" "Johnson" "Johnson" "Smith" "Johnson"
[21] "Smith" "Johnson" "Smith" "Smith" "Smith"
[26] "Smith" "Smith" "Smith" "Johnson" "Johnson"
[31] "Johnson" "Smith" "Smith" "Smith" "Johnson"
[36] "Smith" "Smith" "Smith" "Johnson" "Johnson"
[41] "Smith" "Smith" "Smith" "Johnson" "Johnson"
[46] "Johnson" "Johnson" "Johnson" "Johnson" "Johnson"
[51] "Johnson" "Johnson" "Johnson" "Johnson" "Johnson"
[56] "Johnson" "Johnson" "Johnson" "Johnson" "Smith"
[61] "Smith" "Smith" "Smith" "Johnson" "Johnson"
[66] "Johnson" "Johnson" "Smith" "Smith" "Johnson"
[71] "Johnson" "Johnson" "Johnson" "Johnson" "Johnson"
[76] "Smith" "Smith" "Johnson" "Smith" "Smith"
CHUL LEE wrote:
> Dear list,
> It is not an issue of S-Plus, but I know someone in this forum can
> help me.
> I have data which have missing cases. I want to replace this missing
> with above value. For example,
>
> Col1 Col2
> Smith 3
> 2.5
> 4
> Johnson 4
> 4.5
> 3
> 2.5
>
>
> In this case, I want to replace Row 2 & 3 in Col1 with Smith, but Row
> 5,6,&7 with Johnson. Is there a easy way to do by Excel or with
> Statistics programs? This data has more than 80,000 rows.
>
> Chul
> --------------------------------------------------------------------
> 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
>
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
--------------------------------------------------------------------
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
|