我有两个向量:
a <- letters[1:5]
b <- c('a','k','w','p','b','b')
Run Code Online (Sandbox Code Playgroud)
现在我想计算向量a
中每个字母出现的次数b
.我想得到:
# 1 2 0 0 0
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
tabulate
适用于整数向量,速度快; 将您的字母与可能的字母的世界相匹配,然后将索引制成表格; 用于length(a)
确保每个可能值都有一个计数.
> tabulate(match(b, a), length(a))
[1] 1 2 0 0 0
Run Code Online (Sandbox Code Playgroud)
这比"明显的"table()解决方案更快
library(microbenchmark)
f0 = function() table(factor(b,levels=a))
f1 = function() tabulate(match(b, a), length(a))
Run Code Online (Sandbox Code Playgroud)
然后
> microbenchmark(f0(), f1())
Unit: microseconds
expr min lq median uq max neval
f0() 566.824 576.2985 582.950 594.4200 798.275 100
f1() 56.816 60.0180 63.305 65.4185 120.441 100
Run Code Online (Sandbox Code Playgroud)
但也更通用,例如,匹配数值而不强制转换为字符串表示.
使b
成为具有由 指定水平的因子a
。不在 中的值a
将变成<NA>
。当您制表时,它们将被丢弃(除非您指定useNA="ifany"
)。
table(factor(b,levels=a))
a b c d e
1 2 0 0 0
Run Code Online (Sandbox Code Playgroud)