Skip to content
Back to formatted view

Raw Message

Message-ID: <CB3C00AC-F843-4B4B-B819-4F869E132DD4@comcast.net>
Date: 2010-01-27T02:02:15Z
From: David Winsemius
Subject: (no subject)
In-Reply-To: <20100125134640.IE314.164389.root@mp13>

On Jan 25, 2010, at 1:46 PM, Chuck White wrote:

> Hello -- I would like to know of a more efficient way of writing the  
> following piece of code. Thanks.
>
> options(stringsAsFactors=FALSE)
> orig <- c(rep('11111111',100000),rep('22222222', 
> 200000),rep('33333333',300000),rep('44444444',400000))
> orig.unique <- unique(orig)
> system.time(df <- as.data.frame(sapply(orig.unique, function(x)  
> ifelse(orig==x, 1, 0))))

 > system.time(df <- model.matrix(~orig))
    user  system elapsed
   3.122   0.204   3.312
Warning message:
In model.matrix.default(~orig) : variable 'orig' converted to a factor
 >
 > options(stringsAsFactors=FALSE)
 > orig <- c(rep('11111111',100000),rep('22222222', 
200000),rep('33333333',300000),rep('44444444',400000))
 > orig.unique <- unique(orig)
 > system.time(df <- as.data.frame(sapply(orig.unique, function(x)  
ifelse(orig==x, 1, 0))))
    user  system elapsed
  18.455   0.872  19.234


>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.