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 7709809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:57:20+00:00 2026-06-01T00:57:20+00:00

The objective is to create indicators for a factor/string variable in a data frame.

  • 0

The objective is to create indicators for a factor/string variable in a data frame. That dataframe has > 2mm rows, and running R on windows, I don’t have the option of using plyr with .parallel=T. So I’m taking the “divide and conquer” route with plyr and reshape2.

Running melt and cast runs out of memory, and using

ddply( idata.frame(items) , c("ID") , function(x){
       (    colSums( model.matrix( ~ x$element - 1) ) > 0   )
} , .progress="text" )    

or

ddply( idata.frame(items) , c("ID") , function(x){
           (    elements %in% x$element   )
    } , .progress="text" )  

does take a while. The fastest approach is the call to tapply below. Do you see a way to speed this up? The %in% statement runs faster than the model.matrix call. Thanks.

set.seed(123)

dd <- data.frame(
  id  = sample( 1:5, size=10 , replace=T ) ,
  prd = letters[sample( 1:5, size=10 , replace=T )]
  )

prds <- unique(dd$prd)

tapply( dd$prd , dd$id , function(x) prds %in% x )
  • 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-01T00:57:21+00:00Added an answer on June 1, 2026 at 12:57 am

    For this problem, the packages bigmemory and bigtabulate might be your friends. Here is a slightly more ambitious example:

    library(bigmemory)
    library(bigtabulate)
    
    set.seed(123)
    
    dd <- data.frame(
      id = sample( 1:15, size=2e6 , replace=T ), 
      prd = letters[sample( 1:15, size=2e6 , replace=T )]
      )
    
    prds <- unique(dd$prd)
    
    benchmark(
    bigtable(dd,c(1,2))>0,
    table(dd[,1],dd[,2])>0,
    xtabs(~id+prd,data=dd)>0,
    tapply( dd$prd , dd$id , function(x) prds %in% x )
    )
    

    And the results of benchmarking (I’m learning new things all the time):

                                                test replications elapsed relative user.self sys.self user.child sys.child
    1                      bigtable(dd, c(1, 2)) > 0          100  54.401 1.000000    51.759    3.817          0         0
    2                    table(dd[, 1], dd[, 2]) > 0          100 112.361 2.065422   107.526    6.614          0         0
    4 tapply(dd$prd, dd$id, function(x) prds %in% x)          100 178.308 3.277660   166.544   13.275          0         0
    3                xtabs(~id + prd, data = dd) > 0          100 229.435 4.217478   217.014   16.660          0         0
    

    And that shows bigtable winning out by a considerable amount. The results are pretty much that all prds are in all IDs, but see ?bigtable for details on the format of the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a simple objective C object in xcode that has
Objective c has release method. I want to create my own method that duplicate
My end objective is to create an applescript that intelligently inserts a bullet point
I need to create an Objective-C method that converts an int into a byte
My objective is create an apache module that will provide RESTful services (i.e., we
I am trying to create an objective c interface that encapsulates the functionality of
I am trying to create this dropdown box that slides down and has the
I want to create an Objective-C base class that performs an operation on all
The objective is to create a custom route so that /undead will go to
My objective is to write a program that can create folders and within those

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.