Q: How to match values in one column and parse the matching value from different columns in R I am really stuck on this problem to work in R. I need to grepl(allgood in column C), and if it is present, I want to parse the values in column A and column B from the row matching the preceding allbad in column C and get the result. 
A          B            C
apple      ball         allbad-cat
                        allgood-car
dog        bark         allbad-pet
                        bull
                        dull 
                        allgood-pet        

result
   A          B            C
apple      ball         allbad-cat
apple      ball         allgood-car
dog        bark         allbad-pet
                        bull
                        dull
dog        bark         allgood-pet   


A: # find the index of column "C" starts with "allgood"
good.idx <- which(grepl("^allgood", df$C))
# find the index of column "C" starts with "allbad"
bad.idx <- which(grepl("^allbad", df$C))

# for each "good" index, find the maximum "bad" index smaller than the "good" index
good.bad.near <- sapply(good.idx, function(x){
    return(max(bad.idx[bad.idx<x]))
})

df$A[good.idx] <- df$A[good.bad.near]
df$B[good.idx] <- df$B[good.bad.near]

Works for your data.
If you have more columns to substitute, you can use the for loop.
for (i in 1:2) {
  df[, i][good.idx] <- df[, i][good.bad.near]
}


A: We can try
library(zoo)
i1 <- !grepl('^(allbad|allgood)', df1$C)
df1[1:2] <-  lapply(df1[1:2], function(x) ifelse(i1, '', 
                           na.locf(replace(x, x=='', NA))))
df1
#      A    B     C
#1 apple ball apple
#2 apple ball apple
#3   dog bark   dog
#4                 
#5                 
#6   dog bark   dog


Or using data.table
library(data.table)
setDT(df1)[A=='', c('A', 'B') := NA][, 
           lapply(.SD, na.locf)][i1, c('A', 'B') := ''][]

data
df1 <- structure(list(A = c("apple", "", "dog", "", "", ""), 
B = c("ball", 
"", "bark", "", "", ""), C = c("allbad-cat", "allgood-car", 
"allbad-pet", 
"bull", "dull", "allgood-pet")), .Names = c("A", "B", "C"), 
 class = "data.frame", row.names = c(NA, -6L))

