'How can I filter expressions like ".*_" which contain underscore in R?
Trying to filter phrases in a vector c("phrase\_", "char phrase_char") results only in "phrase\_".
I tried it e.g. with grep and startsWith the "char phrase_char") wasn't found.
Solution 1:[1]
If you are talking about vector elements containing specific characters you can use grep().
Example:
test <- c("phrase_", "char phrase_char", "char char2 phrase_","only char char2")
grep("*phrase_*",test)
# [1] 1 2 3
grepl("*phrase_*",test)
# [1] TRUE TRUE TRUE FALSE
test[grep("*phrase_*",test)]
# [1] "phrase_" "char phrase_char" "char char2 phrase_"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jpsmith |
