Fastest way to count occurrences of each unique element

前端 未结 2 715
醉酒成梦
醉酒成梦 2020-12-05 19:45

What is the fastest way to compute the number of occurrences for each unique element in a vector in R?

So far, I\'ve tried the following five functions:



        
2条回答
  •  醉梦人生
    2020-12-05 20:02

    There's almost nothing that will beat tabulate() provided you can meet the initial conditions.

    x <- sample(1:100, size=1e7, TRUE)
    system.time(tabulate(x))
    #  user  system elapsed 
    # 0.071   0.000   0.072 
    

    @dickoa adds a few more notes in the comments as to how to get the appropriate output, but tabulate as a workhorse function is the way to go.

提交回复
热议问题