R - Select Elements from list that meet the criteria
问题 I had a tough time selecting elements from a list that meet a function. So documenting the same with a solution. check.digits <- function(x){ grepl('^(\\d+)$' , x) } x = "741 abc pqr street 71 15 41 510741" lx = strsplit(x, split = " ", fixed = TRUE) lapply(lx, check.digits) This does not work - lx[[1]][c(lapply(lx, check.digits))] Use - lx[[1]][sapply(lx, check.digits)] thanks!!! 回答1: Given what you're after, perhaps you should just use gregexpr + regmatches : regmatches(x, gregexpr("\\d+",