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

  • SEARCH
  • Home
  • 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 8082399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:04:50+00:00 2026-06-05T17:04:50+00:00

I wonder whether someone has an idea for how to count combinations like the

  • 0

I wonder whether someone has an idea for how to count combinations like the following in a better way than I’ve thought of.

> library(lubridate)
> df <- data.frame(x=sample(now()+hours(1:3), 100, T), y=sample(1:4, 100, T))
> with(df, as.data.frame(table(x, y)))
                     x y Freq
1  2012-06-15 00:10:18 1    5
2  2012-06-15 01:10:18 1    9
3  2012-06-15 02:10:18 1    8
4  2012-06-15 00:10:18 2    9
5  2012-06-15 01:10:18 2   10
6  2012-06-15 02:10:18 2   12
7  2012-06-15 00:10:18 3    7
8  2012-06-15 01:10:18 3    9
9  2012-06-15 02:10:18 3    6
10 2012-06-15 00:10:18 4    5
11 2012-06-15 01:10:18 4   14
12 2012-06-15 02:10:18 4    6

I like that format, but unfortunately when we ran x and y through table(), they got converted to factors. In the final output they can exist quite nicely as their original type, but getting there seems problematic. Currently I just manually fix all the types afterward, which is really messy because I have to re-set the timezone, and look up the percent-codes for the default date format, etc. etc.

It seems like an efficient solution would involve hashing the objects, or otherwise mapping integers to the unique values of x and y so we can use tabulate(), then mapping back.

Ideas?

  • 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-05T17:04:51+00:00Added an answer on June 5, 2026 at 5:04 pm

    Here’s data.table version that preserves the column classes:

    library(data.table)
    
    dt <- data.table(df, key=c("x", "y"))
    dt[, .N, by=key(dt)]
    #                       x y  N
    #  1: 2012-06-14 18:10:22 1  8
    #  2: 2012-06-14 18:10:22 2 10
    #  3: 2012-06-14 18:10:22 3  8
    #  4: 2012-06-14 18:10:22 4  8
    #  5: 2012-06-14 19:10:22 1  6
    #  6: 2012-06-14 19:10:22 2  8
    #  7: 2012-06-14 19:10:22 3  6
    #  8: 2012-06-14 19:10:22 4  6
    #  9: 2012-06-14 20:10:22 1 15
    # 10: 2012-06-14 20:10:22 2  5
    # 11: 2012-06-14 20:10:22 3 12
    # 12: 2012-06-14 20:10:22 4  8
    
    str(dt[, .N, by=key(dt)])
    # Classes ‘data.table’ and 'data.frame':  12 obs. of  3 variables:
    #  $ x: POSIXct, format: "2012-06-14 18:10:22" "2012-06-14 18:10:22" ...
    #  $ y: int  1 2 3 4 1 2 3 4 1 2 ...
    #  $ N: int  8 10 8 8 6 8 6 6 15 5 ...
    

    Edit in response to follow-up question

    To count the number of appearances of all possible combinations of the observed factor levels (including those which don’t appear in the data), you can do something like the following:

    dt<-dt[1:30,]  # Make subset of dt in which some factor combinations don't appear
    
    ii <- do.call("CJ", lapply(dt, unique))  # CJ() is similar to expand.grid()
    dt[ii, .N]
    #                      x y N
    # 1: 2012-06-14 22:53:05 1 8
    # 2: 2012-06-14 22:53:05 2 7
    # 3: 2012-06-14 22:53:05 3 9
    # 4: 2012-06-14 22:53:05 4 5
    # 5: 2012-06-14 23:53:05 1 1
    # 6: 2012-06-14 23:53:05 2 0
    # 7: 2012-06-14 23:53:05 3 0
    # 8: 2012-06-14 23:53:05 4 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wonder whether someone can help me with the following one... I have a struct
I wonder whether someone can help me please. I'm using the following script to
I wonder whether someone could help me please. I'm put together the following form
I wonder whether someone can help me please. I've put together the following which
I wonder whether someone can help me please. I have the following three tables:
I wonder whether someone may be able to help me please. I'm using Aurigma
I wonder whether someone could help me please. I'm using Image Uploader from Aurigma,
I wonder whether someone may be able to help me please. I've put together
I wonder whether someone may be able to help me please. Using some excellent
I wonder whether someone can help me please. I've created this form which allows

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.