Re: [R] "unsparse" a vector

2012-02-09 Thread Petr Savicky
On Wed, Feb 08, 2012 at 05:01:01PM -0500, Sam Steingold wrote: > loop is too slow. > it appears that sparseMatrix does what I want: > > ll <- lapply(l,length) > i <- rep(1:4, ll) > vv <- unlist(l) > j1 <- as.factor(substring(vv,1,1)) > t <- table(j1) > j <- position of elements of j1 in names(t) >

Re: [R] "unsparse" a vector

2012-02-08 Thread Sam Steingold
loop is too slow. it appears that sparseMatrix does what I want: ll <- lapply(l,length) i <- rep(1:4, ll) vv <- unlist(l) j1 <- as.factor(substring(vv,1,1)) t <- table(j1) j <- position of elements of j1 in names(t) sparseMatrix(i,j,x=as.numeric(substring(vv,2,2)), dimnames = names(t)) so, the qu

Re: [R] "unsparse" a vector

2012-02-08 Thread William Dunlap
rhead for such small input vectors). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Sam Steingold > Sent: Wednesday, February 08, 2012 12:56 PM > To: r-hel

Re: [R] "unsparse" a vector

2012-02-08 Thread Petr Savicky
On Wed, Feb 08, 2012 at 03:56:12PM -0500, Sam Steingold wrote: > To be clear, I can do that with nested for loops: > > v <- c("A1B2","A3C4","B5","C6A7B8") > l <- strsplit(gsub("(.{2})","\\1,",v),",") > d <- data.frame(A=vector(length=4,mode="integer"), > B=vector(length=4,mode="int

Re: [R] "unsparse" a vector

2012-02-08 Thread Bert Gunter
Sorry, it's in package compiler, now part of the standard distro. My bad. -- Bert On Wed, Feb 8, 2012 at 1:23 PM, Sam Steingold wrote: >> * Bert Gunter [2012-02-08 13:02:00 -0800]: >> >> A few extra minutes of computing time to save many more minutes of >> programming time seems a reasonable tr

Re: [R] "unsparse" a vector

2012-02-08 Thread Sam Steingold
> * Bert Gunter [2012-02-08 13:02:00 -0800]: > > A few extra minutes of computing time to save many more minutes of > programming time seems a reasonable tradeoff. in this case, many hours of computing time. > see ?compile to compile your Error: object 'compile' not found -- Sam Steingold (

Re: [R] "unsparse" a vector

2012-02-08 Thread Bert Gunter
Sam: On Wed, Feb 8, 2012 at 12:56 PM, Sam Steingold wrote: > To be clear, I can do that with nested for loops: > > v <- c("A1B2","A3C4","B5","C6A7B8") > l <- strsplit(gsub("(.{2})","\\1,",v),",") > d <- data.frame(A=vector(length=4,mode="integer"), >                B=vector(length=4,mode="integer

Re: [R] "unsparse" a vector

2012-02-08 Thread Bert Gunter
I suspect there are cleverer ways to do it, especially using packages like stringr and gsubfn, but using base tools, you can hack it without too much effort: ?gregexpr is the key. To get started (x is your example vector of character strings): > gregexpr("[[:alpha:]]+[[:digit:]]+",x) [[1]] [1] 1

Re: [R] "unsparse" a vector

2012-02-08 Thread Sam Steingold
To be clear, I can do that with nested for loops: v <- c("A1B2","A3C4","B5","C6A7B8") l <- strsplit(gsub("(.{2})","\\1,",v),",") d <- data.frame(A=vector(length=4,mode="integer"), B=vector(length=4,mode="integer"), C=vector(length=4,mode="integer")) for (i in 1:len

[R] "unsparse" a vector

2012-02-08 Thread Sam Steingold
Suppose I have a vector of strings: c("A1B2","A3C4","B5","C6A7B8") [1] "A1B2" "A3C4" "B5" "C6A7B8" where each string is a sequence of pairs (fixed width, in this example both value and name are 1 character, in reality the column name is 6 chars and value is 2 digits). I need to convert it