I'm trying to rewrite a loop as an lapply statement, but I am getting stuck because I cannot figure out how to incorporate the index within the function. I recently asked a similar question on SO and received an elegant response, but the response doesn't generalize to this problem.
I'm working with a set of records, and the records are structured in a long format. I can identify each unique set of records by a unique string. The rows that I want to fix always occurs exactly two rows after these unique strings.
Here is the reproducible data:
text <- c("_____", "A: aaa", "bbb", "C: cccc", "D: dddd",
"_____", "A: aaa:aaa", "bbb", "C: ccc", "D: dddd", "E: eeee",
"_____", "A: aaa", "bbb:bbb", "C: ccc", "D: dddd")
And here is the loop that does what I need it to do. It works just fine on a very small data set, but I have to apply this logic in a few different ways to a few hundred thousand rows of data -- a more efficient method is definitely needed!
for(i in 3:length(text)){
text[i] <- ifelse(grepl("\\_{5}", text[i-2]) == TRUE,
paste("B: ", text[i], sep=""), text[i])
text
}
Of course, feel free to redirect if there are existing problems on SO that I did not identify. Thanks in advance.