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))))
Something along these lines might do the trick: orig <- rep(sapply(seq(from=11111111, to=44444444, by=11111111), as.character), times=c(1, 2, 3, 4)) I've shortened the number of repetitions, so you can test it out and see if it's what you're looking for (just change the values in the vector assigned to the "times" argument). It might be helpful to know a bit more about the specific problem you're trying to solve though. Kyle H. Ambert Fellow, National Library of Medicine Department of Medical Informatics & Clinical Epidemiology Oregon Health & Science University On Mon, Jan 25, 2010 at 10:46 AM, Chuck White <chuckwhite8@charter.net>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)))) > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
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.
Apparently Analagous Threads
- splitting a factor column into binary columns for each factor
- splitting a factor column into binary columns for each level
- How to concatenate Ogg in the browser JS?
- How to concatenate Ogg in the browser JS?
- Samba 4.4.8 AD member ads / nss fails to find group id