4

我有一个带有字符串值列的大型数据框(约 600K 行)(链接)

doc_id,link
1,http://example.com
1,http://example.com
2,http://test1.net
2,http://test2.net
2,http://test5.net
3,http://test1.net
3,http://example.com
4,http://test5.net

我想计算某个字符串值在帧中出现的次数。结果应如下所示:

link, count
http://example.com, 3
http://test1.net, 2
http://test2.net, 1
http://test5.net, 2

在 R 中有没有一种有效的方法来做到这一点?由于帧大小,将帧转换为矩阵不起作用。目前我正在使用 plyr 包,但这太慢了。

4

1 回答 1

5

table函数计算出现次数 - 与ddply. 所以,可能是这样的:

# some sample data
set.seed(42)
df <- data.frame(doc_id=1:10, link=sample(letters[1:3], 10, replace=TRUE))

cnt <- as.data.frame(table(df$link))
# Assign appropriate names (optional)
names(cnt) <- c("link", "count")
cnt

这给出了以下输出:

  link count
1    a     2
2    b     3
3    c     5
于 2011-08-15T15:01:17.453 回答