我刚刚写了一个函数,但是我想知道在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)
举个例子,grepl("[a-z]",x)
是什么意思?
> grepl("[a-z]",x)
[1] FALSE TRUE TRUE FALSE TRUE TRUE FALSE
为什么要把它搞得那么难呢?
> x %in% letters
[1] FALSE TRUE TRUE FALSE TRUE TRUE FALSE
不需要自己编写函数。
另一种使用值而不是逻辑索引作为结果的方法是将字母命名为它们本身,并使用“[”作为索引x:
names(letters) <- letters
letters[x]
#<NA> w k <NA> y c <NA>
# NA "w" "k" NA "y" "c" NA