如何在两个向量中找到相同元素的数量?

use*_*549 4 r

我有两个向量:

 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)

我该怎么办?

Mar*_*gan 5

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)

但也更通用,例如,匹配数值而不强制转换为字符串表示.


Ben*_*ker 4

使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)