Re: [R] Expanding matrix into dummies

2015-12-23 Thread Dimitri Liakhovitski
Thank you very much, Marc and Bert - this is great! On Tue, Dec 22, 2015 at 7:14 PM, Bert Gunter wrote: > ... Perhaps worth noting is that the row indices can be created > directly without row(): > > result[cbind(rep.int(seq_len(6),5), as.vector(x))] <- 1 > > but the downside is that you have to

Re: [R] Expanding matrix into dummies

2015-12-22 Thread Bert Gunter
... Perhaps worth noting is that the row indices can be created directly without row(): result[cbind(rep.int(seq_len(6),5), as.vector(x))] <- 1 but the downside is that you have to know that a matrix is a vector "stored" in column major order. I find this arcane detail quite handy, though. Cheer

Re: [R] Expanding matrix into dummies

2015-12-22 Thread Marc Schwartz
> On Dec 22, 2015, at 4:42 PM, Dimitri Liakhovitski > wrote: > > # I have a matrix x: > > k <- 20 > N <- 5 > set.seed(123) > x <- matrix(c(sample(1:k, N, replace = F), > sample(1:k, N, replace = F), > sample(1:k, N, replace = F), > sample(1:k, N, replace

[R] Expanding matrix into dummies

2015-12-22 Thread Dimitri Liakhovitski
# I have a matrix x: k <- 20 N <- 5 set.seed(123) x <- matrix(c(sample(1:k, N, replace = F), sample(1:k, N, replace = F), sample(1:k, N, replace = F), sample(1:k, N, replace = F), sample(1:k, N, replace = F), sample(1:k, N, repl