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
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
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,]
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
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 =
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
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
7 matches
Mail list logo