editor 发表于 2013-3-19 09:08:48

R语言 如何获取一组数据的频数??—R-China小编问题(34)

本帖最后由 editor 于 2013-5-25 11:27 编辑

相信大家在数据分析过程中,或多或少的遇到过这样的问题:
已知一堆数字,想知道这堆数据中每一个数字出现的次数。
那么对于这样的简单的问题,如何操作呢?
其实说简单也不简单,因为R存在内存消耗过大的问题。

editor 发表于 2013-3-19 09:20:18

小编在此,给出两种方式,就算抛砖引玉了。下面还请各位高手给出其他方式
1 当这组数据不大的时候,比如百万级以下的时候,可以用table()来实现。
x<-sample(1:10,size=1e+6,replace=T);
table(x);
x
   1      2      3      4      5      6      7      8      9   10
99867 100014 100410 100139 100164 10003599898 100183 10004399247

2 当数据量比较大的时候,比如数量级在百万之上,达到千万的时候。大家可以用tabulate()处理
x<-sample(1:10,size=1e+7,replace=T);
b<-tabulate(x);
names(b)<-1:10
b
      1       2       3       4       5       6       7       8       9      10
998164999179 1001283 1002016998593999229 1002417 1000089999750999280

注释:
tabulate()
##tabulate takes the integer-valued vector bin and counts the number of times each integer occurs in it.

bin a numeric vector (of positive integers), or a factor. Long vectors are supported.

nbins the number of bins to be used.

hou0922 发表于 2016-6-9 00:22:21

对的,table()函数特别好用

manpoR 发表于 2016-7-5 16:23:57

table函数有点慢。:D

amelinda 发表于 2023-5-16 14:28:01

已知一堆数字,想知道这堆数据中每一个数字出现的次数。
那么对于这样的简单的幸运飞艇走势图双色球开奖结果
                                                   澳洲幸运20稳赚技巧问题,如何操作呢?
其实说简单也不简单,因为R存在内存消耗过大的问题。
页: [1]
查看完整版本: R语言 如何获取一组数据的频数??—R-China小编问题(34)