The following, locf2(), does what you want using only base R functions: locf2 <- function(x, initial=NA, IS_BAD = is.na) { # Replace 'bad' values in 'x' with last previous non-bad value. # If no previous non-bad value, replace with 'initial'. stopifnot(is.function(IS_BAD)) good <- !IS_BAD(x) stopifnot(is.logical(good), length(good) == length(x), !anyNA(good)) i <- cumsum(good) x <- x[c(1,which(good))][i+1] x[i==0] <- initial x }

In your example, you can use IS_BAD=function(x)x=="" or function(x)is.na(x) | x=="" > df1 <- data.frame(nam=c("mandy","", "John","","","","Zara",""), + val=c(1,2,3,4,5,6,7,8)) > cbind(df1, FilledNam = locf2(df1$nam, IS_BAD=function(x)x=="")) nam val FilledNam 1 mandy 1 mandy 2 2 mandy 3 John 3 John 4 4 John 5 5 John 6 6 John 7 Zara 7 Zara 8 8 Zara > Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Aug 9, 2017 at 2:20 PM, Ek Esawi <esaw...@gmail.com> wrote: > Hi All— > > I was looking at a posting from June-17. I managed to solve it. However, > when I changed the example in the posting, my solution will work only once > at a time which was mentioned by Jim Lemon on his response to the original > posting. This means that my solution will have to be repeated as many times > as the maximum number of spaces on each gap; something that may not work > well for large files. > > I am trying to solve the new example with base R functions only. I thought > of splitting the first column to multiple lists and use one of the apply > functions, but was not successful. > > Would apprecaite some hints on how to go about it. > > Thanks as always in advance—EK > > The posted data frame from the original posting: > names val > 1 Mandy 1 > 2 2 > 3 John 2 > 4 2 > > My modified data frame: > val <- c(1,2,3,4,5,6,7,8) > nam <- c("mandy","", "John","","","","Zara","") > df1 <- data.frame(nam,val) > > nam val > 1 mandy 1 > 2 2 > 3 John 3 > 4 4 > 5 5 > 6 6 > 7 Zara 7 > 8 8 > > My code for solving the original data farme > > which(df1$nam=="") > > a <- which(df1$nam=="") > > df1$nam[a] <- df1$nam[a-1] > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.