有没有办法在 R 中使用 xapply 对这个操作进行矢量化

Is there a way to vectorize this operation using xapply in R

我有一个向量

a <- c("there and", "walk and", "and see", "go there", "was i", "and see", 
"i walk", "to go", "to was")

和一个数据框 bg,其中

bg <- data.frame(term=c("there and", "walk and", "and see", "go there", "was i", "and see",
"i walk", "to go", "to was"), freq=c(1,1,2,1,1,2,1,1,1))

我需要使用 sapply、tapply、vapply 或 apply 等为以下代码创建矢量化版本

 d <- NULL
 for(i in 1:length(a)){
     temp <- filter(bg,term==a[i])
     d <- rbind(d,temp)
 }

需要在term==a[i]时搜索bg数据并创建数据框d

我需要一个矢量版本,因为循环在 R 中非常慢。

这是示例数据

> bg
       term freq
1 there and    1
2  walk and    1
3   and see    2
4  go there    1
5     was i    1
6   and see    2
7    i walk    1
8     to go    1
9    to was    1

>d
       term freq
1 there and    1
2  walk and    1
3   and see    2
4   and see    2
5  go there    1
6     was i    1
7   and see    2
8   and see    2
9    i walk    1
10    to go    1
11   to was    1

谢谢

这实际上变成了一个 merge 操作,稍作调整以确保行顺序遵循 a 中的顺序:

out <- merge(bg, list(term=a, sortid=seq_along(a)), by="term")
out[order(out$sortid),]

#        term freq sortid
#7  there and    1      1
#10  walk and    1      2
#1    and see    2      3
#3    and see    2      3
#5   go there    1      4
#11     was i    1      5
#2    and see    2      6
#4    and see    2      6
#6     i walk    1      7
#8      to go    1      8
#9     to was    1      9

或在 data.table 1.9.5 中,向@akrun 致敬:

library(data.table)
out <- data.table(term=a, sortid=seq_along(a))[setDT(bg), on='term']
out[order(out$sortid)]

dplyr:

left_join(data.frame(term=a), bg)