问题
给出一个载体列表
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"
如何找到由R[0-9]
组合组成的条目?
所以在这个例子中它将是lst[[4]]
一些规则
R[0-9]
个值。 R[0-9]
值的数量将在每个列表中更改R[0-9]
元素中使用的值数据
dput(lst)
list("{{=Number}}", "", c("Auto", "Small", "Medium", "Large"),
c("R1", "R2", "R3", "R4", "R5"), c("1", "2", "3", "4", "5"
))
答案 0 :(得分:1)
@jenesaisquoi提供了一个很好的解决方案:
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"
答案 1 :(得分:1)
另一种可能性:
sapply(lst, function(x) all(x %in% paste0("R", 0:9)))
# [1] FALSE FALSE FALSE TRUE FALSE