Re: [R] Column-by-column division

2021-03-03 Thread Harold Doran
ay, March 3, 2021 3:19 PM To: Harold Doran Cc: Steven Yen ; R-help Mailing List Subject: Re: [R] Column-by-column division Why not use standard matrix multiplication which is straightforward here: x %*% diag(1/s) HTH, Eric On Wed, Mar 3, 2021 at 7:13 AM Harold Doran mailto:harold.do...@cambi

Re: [R] Column-by-column division

2021-03-03 Thread Eric Berger
Why not use standard matrix multiplication which is straightforward here: x %*% diag(1/s) HTH, Eric On Wed, Mar 3, 2021 at 7:13 AM Harold Doran < harold.do...@cambiumassessment.com> wrote: > To make sure the scalar is used instead of using the recycled vector s, > maybe like this > > x <- matr

Re: [R] Column-by-column division

2021-03-03 Thread Harold Doran
To make sure the scalar is used instead of using the recycled vector s, maybe like this x <- matrix(1:20, nrow=10) s <- c(1,2) sapply(1:2, function(i) x[,i]/s[i]) -Original Message- From: R-help On Behalf Of Steven Yen Sent: Wednesday, March 3, 2021 6:00 AM To: R-help Mailing List Sub

Re: [R] Column-by-column division

2021-03-03 Thread Steven Yen
Thanks to all. sweep is convenient. On 2021/3/3 下午 07:16, Rui Barradas wrote: Hello, I forgot about sweep: sweep(x, 2, s, '/') sweep(x, 2, 1:4, '/') Hope this helps, Rui Barradas Às 11:12 de 03/03/21, Rui Barradas escreveu: Hello, Maybe define an infix operator? `%!%` <- function(x, y

Re: [R] Column-by-column division

2021-03-03 Thread Rui Barradas
Hello, I forgot about sweep: sweep(x, 2, s, '/') sweep(x, 2, 1:4, '/') Hope this helps, Rui Barradas Às 11:12 de 03/03/21, Rui Barradas escreveu: Hello, Maybe define an infix operator? `%!%` <- function(x, y) {   stopifnot(ncol(x) == length(y))   t(t(x)/y) } x <- matrix(1:20, ncol =

Re: [R] Column-by-column division

2021-03-03 Thread Rui Barradas
Hello, Maybe define an infix operator? `%!%` <- function(x, y) { stopifnot(ncol(x) == length(y)) t(t(x)/y) } x <- matrix(1:20, ncol = 2) s <- 1:2 x %!% s x %!% 1:4 Hope this helps, Rui Barradas Às 11:00 de 03/03/21, Steven Yen escreveu: I have a 10 x 2 matrix x. Like to divide the fir