r - find list item where all values match a regular expression -
question
given list of vectors
lst [[1]] [1] "{{=number}}" [[2]] [1] "" [[3]] [1] "auto" "small" "medium" "large" [[4]] [1] "r1" "r2" "r3" "r4" "r5" [[5]] [1] "1" "2" "3" "4" "5"
how find entry consists of set of r[0-9]
combinations?
so in exmample lst[[4]]
some rules
- the list item i'm after contain
r[0-9]
values. - the number of
r[0-9]
values change in each list - the next list item set of numbers, each being values used in
r[0-9]
element
data
dput(lst) list("{{=number}}", "", c("auto", "small", "medium", "large"), c("r1", "r2", "r3", "r4", "r5"), c("1", "2", "3", "4", "5" ))
@jenesaisquoi has provided nice solution:
vapply(lst, function(x) all(grepl('^r[0-9]', x)), logical(1)) # [1] false false false true false ## e.g # lst[vapply(lst, function(x) all(grepl('^r[0-9]', x)), logical(1))] # [[1]] # [1] "r1" "r2" "r3" "r4" "r5"
Comments
Post a Comment