You could use strsplit
and setdiff
, I added an additional edge case to your sample data :
a<-c("hour","four","ruoh", "six", "high", "our","oouh")
a[nchar(a) == 4 &
lengths(lapply(strsplit(a,""),function(x) setdiff(x, c("o","u","h")))) == 1]
# [1] "hour" "ruoh"
or grepl
:
a[nchar(a) == 4 & !rowSums(sapply(c("o","u","h"), Negate(grepl), a))]
# [1] "hour" "ruoh" "oouh"
sapply(c("o","u","h"), Negate(grepl), a)
gives you a matrix of which word doesn't contain each letter, then the rowSums
acts like any
applied by row, as it will be coerced to logical.