[英]how to filter only vectors that contain all uppercase letters in all the strings in R
I need to filter rows that are uppercase in R.我需要过滤 R 中大写的行。 I managed to use the following code:
我设法使用以下代码:
filter(str_detect(fruit, "^[:upper:]+$"))
However, some of the values of the column "fruit" contain two or three strings, and the code above only works for the cases when there is only one string.但是,“fruit”列的某些值包含两个或三个字符串,上面的代码仅适用于只有一个字符串的情况。 I can't share the data, but this example works for my purposes (only the str_detect part)
我无法共享数据,但此示例适用于我的目的(仅 str_detect 部分)
fruit <- c("apple", "ORANGE", "kiwi" ,"TWO PEARS", "A BIG PINEAPPLE", "LEMON")
str_detect(fruit, "^[:upper:]+$")
[1] FALSE TRUE FALSE FALSE FALSE TRUE
What I want is to also be able to identify "TWO PEARS" and "A BIG PINEAPPLE".我想要的是也能够识别“两个梨”和“一个大菠萝”。 Could you please help me?
请你帮助我好吗?
Many thanks!非常感谢!
Try to include the space character class.尝试包含空格字符 class。
stringr::str_detect(fruit, "^[[:upper:][:space:]]+$")
#[1] FALSE TRUE FALSE TRUE TRUE TRUE
Following the comment to the question, negate uppercase:在对问题的评论之后,否定大写:
stringr::str_detect(fruit, "^[^[:lower:]]+$")
#[1] FALSE TRUE FALSE TRUE TRUE TRUE
We can use grep
in base R
我们可以在
base R
中使用grep
grep("^[A-Z ]+$", fruit, value = TRUE)
#[1] "ORANGE" "TWO PEARS" "A BIG PINEAPPLE" "LEMON"
To get the other elements获取其他元素
grep("^[A-Z ]+$", fruit, value = TRUE, inverse = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.