I have the following data frame:real_test <- as.data.frame(matrix(rep(NA, 12), nrow = 4, ncol = 3))real_test$V1 <- c(1 NA NA NA, 1 2 NA NA, 1 NA 3 NA, 1 2 3 NA)real_test$V2 <- c(1, 2, 2, 3)real_test$V3 <- c(1, 1, 1, 1)names(real_test) <- c(BUCKET, POS_BREADTH, freq)For each element of real_test$BUCKET I want to return the next real_test$BUCKET using the following approach:This function returns all non-NA elements of any given x    pat <- function(x){      x <- as.vector(read.table(text = x, sep =  , colClasses = numeric))      ready <- x[!is.na(x)]      return(paste(ready, sep = ,))    }This function looks for an item with +1 POS_BREADTHsegment <- function(x, y){  inter <- real_test[colSums(outer(paste(\\b, pat(x), \\b, sep = ), real_test$BUCKET, Vectorize(grepl))) == length(pat(x)),]  inter <- inter[inter$POS_BREADTH == y + 1, c(1,3)]  return(inter$BUCKET[which.max(inter$freq)])}And I use mapply to pass the column names as x and y in a function:real_test$TARTGET <- mapply(segment, real_test$BUCKET, real_test$POS_BREADTH)Is there a faster way? The \\bpat(x)\\b is intentional since I want to have a full match in grepl.
Replacing mapply to gain speed
performance;regex;r