Owen Jones
2008-Jul-28 18:10 UTC
[R] Fill in NA values in vector with previous character/factor
I have a vector of data (species names) interspersed with NA values and I want a function to "fill in the blanks", replacing NA values with whatever the last species name was. For example the vector: "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA. should evaluate to: "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" I tried to use rle() in a function to do this but have hit a brick wall. How would YOU do this? Many thanks, Owen
Gabor Grothendieck
2008-Jul-28 19:24 UTC
[R] Fill in NA values in vector with previous character/factor
Try this: library(zoo) x <- c("A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA) na.locf(x) On Mon, Jul 28, 2008 at 2:10 PM, Owen Jones <owen.jones at imperial.ac.uk> wrote:> I have a vector of data (species names) interspersed with NA values and I > want a function to "fill in the blanks", replacing NA values with whatever > the last species name was. > > For example the vector: > > "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA. > > should evaluate to: > > "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" > > > I tried to use rle() in a function to do this but have hit a brick wall. > > How would YOU do this? > > Many thanks, > > Owen > > ______________________________________________ > R-help at r-project.org 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. >
Dimitris Rizopoulos
2008-Jul-28 19:26 UTC
[R] Fill in NA values in vector with previous character/factor
try function na.locf() from package 'zoo', i.e., library(zoo) x <- c("A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA) na.locf(x) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://perswww.kuleuven.be/dimitris_rizopoulos/ Quoting Owen Jones <owen.jones at imperial.ac.uk>:> I have a vector of data (species names) interspersed with NA values and > I want a function to "fill in the blanks", replacing NA values with > whatever the last species name was. > > For example the vector: > > "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA. > > should evaluate to: > > "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" > > > I tried to use rle() in a function to do this but have hit a brick wall. > > How would YOU do this? > > Many thanks, > > Owen > > ______________________________________________ > R-help at r-project.org 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.Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Yasir Kaheil
2008-Jul-28 19:36 UTC
[R] Fill in NA values in vector with previous character/factor
maybe this is easiest way to do it: x<-c("A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA); x[is.na(x)]<- "D"; x thanks y Owen Jones-3 wrote:> > I have a vector of data (species names) interspersed with NA values > and I want a function to "fill in the blanks", replacing NA values > with whatever the last species name was. > > For example the vector: > > "A","B",NA,NA,"C",NA,NA,NA,NA,"D",NA,NA. > > should evaluate to: > > "A" "B" "B" "B" "C" "C" "C" "C" "C" "D" "D" "D" > > > I tried to use rle() in a function to do this but have hit a brick wall. > > How would YOU do this? > > Many thanks, > > Owen > > ______________________________________________ > R-help at r-project.org 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. > >----- Yasir H. Kaheil Columbia University -- View this message in context: http://www.nabble.com/Fill-in-NA-values-in-vector-with-previous-character-factor-tp18697427p18697756.html Sent from the R help mailing list archive at Nabble.com.