Take the data from this sample:
data.frame(a_1=c("Apple","Grapes","Melon","Peach"),a_2=c("Nuts","Kiwi","Lime","Honey"),a_3=c("Plum","Apple",NA,NA),a_4=c("Cucumber",NA,NA,NA)) a_1 a_2 a_3 a_4 1 Apple Nuts Plum Cucumber 2 Grapes Kiwi Apple <NA> 3 Melon Lime <NA> <NA> 4 Peach Honey <NA> <NA>
Basically, I want to run grep in the last column of every row that is not NA. So my x in grep ("pattern", x) should be:
Cucumber Apple Lime Honey
I have an integer that tells me which a_N is the last:
numcol <- rowSums(!is.na(df[,grep("(^a_)\\d", colnames(df))]))
So far I have tried something similar in combination with ave (), apply () and dplyr:
grepl("pattern",df[,sprintf("a_%i",numcol)])
However, I cannot get it to work. Keep in mind that my dataset is very large, so I was hoping it would be a vector solution or mb dplyr. Help would be greatly appreciated.
/ e: Thank you, this is a really good solution. My thinking was too complicated. (regex associated with my more specific data)