[18971] in s-news-athena
[S] filling NA
daemon@ATHENA.MIT.EDU (fan@katan.cybercom.net)
Wed Aug 18 18:42:43 1999
From: fan@katan.cybercom.net
Date: Wed, 18 Aug 1999 18:35:09 -0400
Message-Id: <199908182235.SAA00292@katan.cybercom.net>
To: s-news@wubios.wustl.edu
"x" is a vector with NA's in it. I want to fill in these missing values and
choose to fill in the number immediately antecedent to it. For example,
> x
[1] 1.728207 NA 2.687416 4.152794 NA NA 6.879287 6.825644
[9] 7.729189 9.689368
I will fill in x[2] with x[1], fill in x[5] and x[6] with x[4], the result
should be
> x
[1] 1.728207 1.728207 2.687416 4.152794 4.152794 4.152794 6.879287 6.825644
[9] 7.729189 9.689368
Is there a way to work on this with S vector operations? I.e., somethings more
concise while having the same effect as the lengthy codes below:
current <- x[1] #assuming x[1] is not
for (i in 1:length(x))
if(x[i]=="NA")
x[i] <- current
else
current <- x[i]
}
Thanks
Fan
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news