计算与 R 中的字符串匹配的因子水平数
Count number of factor levels that match a character string in R
我有一个具有多个级别的因子,我正在尝试获取因子级别包含给定字符串的级别数。
考虑到这个因素:
exdata <- factor(c("Test1","Test2","Sample1","Sample2","Test1","Test2","Sample3"))
我想在包含 "Sample" 或 "Test."
的 exdata 中查找级别数
到目前为止,我的解决方案是使用 nlevels
、droplevels
和 grep
:
nlevels(droplevels(exdata[grep("Test",exdata)]))
# Correct/intended answer is 2
nlevels(droplevels(exdata[grep("Sample",exdata)]))
# Correct/intended answer is 3
有更简洁的方法吗?
使用levels()
levels(exdata)
# [1] "Sample1" "Sample2" "Sample3" "Test1" "Test2"
所以你可以做两个单独的调用...
length(grep("Sample", levels(exdata), fixed = TRUE))
# [1] 3
length(grep("Test", levels(exdata), fixed = TRUE))
# [1] 2
或者一口气...
f <- function(x) length(grep(x, levels(exdata), fixed = TRUE))
sapply(c("Sample", "Test"), f)
# Sample Test
# 3 2
我有一个具有多个级别的因子,我正在尝试获取因子级别包含给定字符串的级别数。
考虑到这个因素:
exdata <- factor(c("Test1","Test2","Sample1","Sample2","Test1","Test2","Sample3"))
我想在包含 "Sample" 或 "Test."
的 exdata 中查找级别数到目前为止,我的解决方案是使用 nlevels
、droplevels
和 grep
:
nlevels(droplevels(exdata[grep("Test",exdata)]))
# Correct/intended answer is 2
nlevels(droplevels(exdata[grep("Sample",exdata)]))
# Correct/intended answer is 3
有更简洁的方法吗?
使用levels()
levels(exdata)
# [1] "Sample1" "Sample2" "Sample3" "Test1" "Test2"
所以你可以做两个单独的调用...
length(grep("Sample", levels(exdata), fixed = TRUE))
# [1] 3
length(grep("Test", levels(exdata), fixed = TRUE))
# [1] 2
或者一口气...
f <- function(x) length(grep(x, levels(exdata), fixed = TRUE))
sapply(c("Sample", "Test"), f)
# Sample Test
# 3 2