gpt4 book ai didi

r - 大型 data.frame 中的高效字符串值计数

转载 作者:行者123 更新时间:2023-12-04 12:17:55 25 4
gpt4 key购买 nike

我有一个带有字符串值列的大数据框(~ 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 包,但这太慢了。

最佳答案

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

关于r - 大型 data.frame 中的高效字符串值计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7066436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com