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
e 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 > Subject: [R] Column-by-column division

Re: [R] Column-by-column division

2021-03-03 Thread Harold Doran
g List Subject: [R] Column-by-column division I have a 10 x 2 matrix x. Like to divide the first column by s[1] and second column by s[2]. The following lines work but are clumsy. Any idea? Thanks. > x   [,1] [,2]  [1,]    1   11  [2,]    2   12  [3,]    3   13  [4,]    4   14  [5,] 

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

[R] Column-by-column division

2021-03-03 Thread Steven Yen
I have a 10 x 2 matrix x. Like to divide the first column by s[1] and second column by s[2]. The following lines work but are clumsy. Any idea? Thanks. > x   [,1] [,2]  [1,]    1   11  [2,]    2   12  [3,]    3   13  [4,]    4   14  [5,]    5   15  [6,]    6   16  [7,]    7   17  [8,]    8