Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-04-02 Thread Rasmus Liland
On 2020-04-03 07:39 +1100, Jim Lemon wrote: | | Hi Rasmus, | Very nice. The R help list is a bit like a | quiz show where the contestants must | balance the alacrity with which they press | their buttons with the confidence that they | have the correct answer. A slow motion game | of wits in

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-04-02 Thread Rasmus Liland
On 2020-04-01 15:33 +1100, Jim Lemon wrote: > Hi Nevil, > It's a nasty piece of work, but: Hi! How about this one: data <- c(rep(1:4, times=3), 2, 1, 3, 2) dimnames <- list(NULL, c("x", "y", "z", "k")) ncol <- length(data)/4 M <- matrix(data=data, nrow=ncol, ncol=ncol, dimnames=d

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-04-02 Thread Jim Lemon
Hi Rasmus, Very nice. The R help list is a bit like a quiz show where the contestants must balance the alacrity with which they press their buttons with the confidence that they have the correct answer. A slow motion game of wits in which the prizes are self-awarded. Jim On Fri, Apr 3, 2020 at 7:

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-03-31 Thread Jim Lemon
Hi Nevil, It's a nasty piece of work, but: M<-matrix(c(1,2,3,4,1,2,3,4,1,2,3,4,2,1,3,2),4,4, dimnames = list(NULL, c("x", "y", "z","k"))) M reprow<-function(x) return(matrix(rep(x,x[length(x)]),nrow=x[length(x)],byrow=TRUE)) toseq<-function(x) return(1:x) j<-unlist(sapply(M[,"k"],toseq)) Mlist<-a

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-03-31 Thread nevil amos
OK sorted - hope these postings might help someone else Any even faster options would be appreciated still #seq() does not work but sequence() does print("rep and sequence") print(system.time({ j<-NULL MOut<-NULL MOut<-M[rep(1:nrow(M), times = M[,4]), ] j<-sequence(M[,4]) MOut<-cbind(M

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-03-31 Thread nevil amos
Well, I found a way to do it partly using rep(), and one loop that makes it 10x or more faster however would still be good to do without the loop at all matrix made slightly beigger (1 rows): M<-matrix(c(1:3 ), 1,3) M<-cbind(M,sample(1:5,size = 1,replace = T)) #Print(M) #Create mat

Re: [R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-03-31 Thread Jeff Newmiller
False premise: rep works fine Mout2 <- cbind(M[ rep(seq.int(nrow(M)),M[,"k"]), c("x","y","z")],unlist(lapply(M[,"k"],seq.int))) On March 31, 2020 6:18:37 PM PDT, nevil amos wrote: >Hi > >I can achieve this using two for loops but it is slow I need to do >this on >many matrices with tens of mil

[R] repeat rows of matrix by number (k) in one colummatrix adding column j with values 1:k

2020-03-31 Thread nevil amos
Hi I can achieve this using two for loops but it is slow I need to do this on many matrices with tens of millions of rows of x,y,z and k What is a faster method to achieve this, I cannot use rep as j changes in each row of the new matrix ### M<-matrix(