是否有一个函数可以查找字符向量中的所有小写字母?

发布于 2024-10-07 09:48:56 字数 243 浏览 0 评论 0原文

我刚刚写了一个,但我想知道 R 中是否已经存在这个函数。

顺便说一句,这是该函数(欢迎提出改进建议):

set.seed(50)
x <- sample(c(letters, LETTERS), 7)

is.lower <- function(x)
{
    unlist(sapply(x, function(x2) {x2 %in% letters}))
}

is.lower(x)

I just wrote one, but I was wondering if one already exists in R.

Here's the function BTW (suggestions for improvement are welcome):

set.seed(50)
x <- sample(c(letters, LETTERS), 7)

is.lower <- function(x)
{
    unlist(sapply(x, function(x2) {x2 %in% letters}))
}

is.lower(x)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱皱眉﹌ 2024-10-14 09:48:56

例如 grepl("[az]",x)

> grepl("[a-z]",x)
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE

为什么要让它变得困难呢?

> x %in% letters
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE

无需创建自己的函数。

grepl("[a-z]",x) for example?

> grepl("[a-z]",x)
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE

And why make it difficult?

> x %in% letters
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE

No need to make your own function.

听你说爱我 2024-10-14 09:48:56

另一种使用值而不是逻辑索引作为结果的方法是将字母命名为它们本身,并使用“[”和 x 作为索引:

 names(letters) <- letters
 letters[x]
#<NA>    w    k <NA>    y    c <NA> 
#  NA  "w"  "k"   NA  "y"  "c"   NA 

Another approach with the values instead of a logical index as the result, would be to name the letters as themselves and use "["with x as the index:

 names(letters) <- letters
 letters[x]
#<NA>    w    k <NA>    y    c <NA> 
#  NA  "w"  "k"   NA  "y"  "c"   NA 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文