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

时间:2015-08-25 04:14:46

标签: r sapply tapply

我有一个载体

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或者应用等为以下代码创建矢量化版本

 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

由于

0 个答案:

没有答案