Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8554183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:49:22+00:00 2026-06-11T14:49:22+00:00

Suppose I have a vector and I don’t know, apriori, its unique elements (here:

  • 0

Suppose I have a vector and I don’t know, apriori, its unique elements (here: 1 and 2).

vec <-
  c(1, 1, 1, 2, 2, 2, 2)

I was interested in knowing is there a better way (or elegant way) of getting the number of unique elements in vec i.e. the same result as table(vec). It doesn’t matter if its a data.frame or a named vector.

R> table(vec)
vec
1 2 
3 4 

Reason: I was curious to know if there is a better way. Also, I noticed that there is a for loop in the base implementation (in addition to .C call). I don’t know if it’s a big concern, but when I do something like

R> table(rep(1:1000,100000))

R takes really long time. I am sure it’s because of the huge number 100000. But is there a way of making it faster?

EDIT This also does a good job in addition to Chase's answer.

R> rle(sort(sampData))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:49:23+00:00Added an answer on June 11, 2026 at 2:49 pm

    This is an interesting problem – I’m curious to see other thoughts on this. Looking at the source for table() reveals that it builds off of tabulate(). tabulate() has a few quirks apparently, namely that it only deals with positive integers and returns an integer vector without names. We can use unique() on our vector to apply the names(). If you need to tabulate zero or negative values, I guess going back and reviewing table() would be necessary as tabulate() doesn’t seem to do that per the examples on the help page.

    table2 <- function(data) {
        x <- tabulate(data)
        y <- sort(unique(data))
        names(x) <- y
        return(x)   
        }
    

    And a quick test:

    > set.seed(42)
    > sampData <- sample(1:5, 10000000, TRUE, prob = c(.3,.25, .2, .15, .1))
    > 
    > system.time(table(sampData))
       user  system elapsed 
      4.869   0.669   5.503 
    > system.time(table2(sampData))
     user  system elapsed 
    0.410   0.200   0.605
    > 
    > table(sampData)
    sampData
          1       2       3       4       5 
    2999200 2500232 1998652 1500396 1001520 
    > table2(sampData)
          1       2       3       4       5 
    2999200 2500232 1998652 1500396 1001520 
    

    EDIT: I just realized there is a count() function in plyr which is another alternative to table(). In the test above, it performs better than table(), and slightly worse than the hack-job solution I put together:

    library(plyr)
     system.time(count(sampData))
       user  system elapsed 
      1.620   0.870   2.483
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have an R vector of unique elements such as x <- c(1,2,3,4,5)
Suppose I have a vector<int> myvec and I want to loop through all of
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose the vector I have is <1, 2, 3>, I want to get the
suppose we have a vector<student> allstudent Now I would like to sort the students
Suppose I have a named vector, bar : bar=c() bar[1997-10-14]=1 bar[2001-10-14]=2 bar[2007-10-14]=1 How can
Suppose I have a vector of points as polar coordinates. Suppose one of those
Suppose I have the following vector: x <- c(8, 6, 9, 9, 7, 3,
I have a need to wrap a vector iterator, but don't like the idea
I can think of three ways of filling a std::vector Suppose we have vector<int>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.