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?? 18 ?[9,]??? 9?? 19 [10,]?? 10?? 20 > s [1] 1 2 > t(t(x)/s) ????? [,1] [,2] ?[1,]??? 1? 5.5 ?[2,]??? 2? 6.0 ?[3,]??? 3? 6.5 ?[4,]??? 4? 7.0 ?[5,]??? 5? 7.5 ?[6,]??? 6? 8.0 ?[7,]??? 7? 8.5 ?[8,]??? 8? 9.0 ?[9,]??? 9? 9.5 [10,]?? 10 10.0
Column-by-column division
7 messages · Rui Barradas, Harold Doran, Steven Yen +1 more
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 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?? 18 ?[9,]??? 9?? 19 [10,]?? 10?? 20
> s
[1] 1 2
> t(t(x)/s)
????? [,1] [,2] ?[1,]??? 1? 5.5 ?[2,]??? 2? 6.0 ?[3,]??? 3? 6.5 ?[4,]??? 4? 7.0 ?[5,]??? 5? 7.5 ?[6,]??? 6? 8.0 ?[7,]??? 7? 8.5 ?[8,]??? 8? 9.0 ?[9,]??? 9? 9.5 [10,]?? 10 10.0
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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 <r-help-bounces at r-project.org> On Behalf Of Steven Yen Sent: Wednesday, March 3, 2021 6:00 AM To: R-help Mailing List <r-help at r-project.org> 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,]??? 5?? 15 ?[6,]??? 6?? 16 ?[7,]??? 7?? 17 ?[8,]??? 8?? 18 ?[9,]??? 9?? 19 [10,]?? 10?? 20 > s [1] 1 2 > t(t(x)/s) ????? [,1] [,2] ?[1,]??? 1? 5.5 ?[2,]??? 2? 6.0 ?[3,]??? 3? 6.5 ?[4,]??? 4? 7.0 ?[5,]??? 5? 7.5 ?[6,]??? 6? 8.0 ?[7,]??? 7? 8.5 ?[8,]??? 8? 9.0 ?[9,]??? 9? 9.5 [10,]?? 10 10.0 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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 = 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 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?? 18 ??[9,]??? 9?? 19 [10,]?? 10?? 20 ?> s [1] 1 2 ?> t(t(x)/s) ?????? [,1] [,2] ??[1,]??? 1? 5.5 ??[2,]??? 2? 6.0 ??[3,]??? 3? 6.5 ??[4,]??? 4? 7.0 ??[5,]??? 5? 7.5 ??[6,]??? 6? 8.0 ??[7,]??? 7? 8.5 ??[8,]??? 8? 9.0 ??[9,]??? 9? 9.5 [10,]?? 10 10.0
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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) {
?? 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 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?? 18 ??[9,]??? 9?? 19 [10,]?? 10?? 20 ?> s [1] 1 2 ?> t(t(x)/s) ?????? [,1] [,2] ??[1,]??? 1? 5.5 ??[2,]??? 2? 6.0 ??[3,]??? 3? 6.5 ??[4,]??? 4? 7.0 ??[5,]??? 5? 7.5 ??[6,]??? 6? 8.0 ??[7,]??? 7? 8.5 ??[8,]??? 8? 9.0 ??[9,]??? 9? 9.5 [10,]?? 10 10.0
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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.doran at cambiumassessment.com> wrote:
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 <r-help-bounces at r-project.org> On Behalf Of Steven Yen Sent: Wednesday, March 3, 2021 6:00 AM To: R-help Mailing List <r-help at r-project.org> 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,] 5 15 [6,] 6 16 [7,] 7 17 [8,] 8 18 [9,] 9 19 [10,] 10 20
> s
[1] 1 2
> t(t(x)/s)
[,1] [,2] [1,] 1 5.5 [2,] 2 6.0 [3,] 3 6.5 [4,] 4 7.0 [5,] 5 7.5 [6,] 6 8.0 [7,] 7 8.5 [8,] 8 9.0 [9,] 9 9.5 [10,] 10 10.0
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Some timings of the different suggestions below
library(microbenchmark) x <- matrix(1:20, nrow=10) s <- c(1,2) option1 <- sapply(1:2, function(i) x[,i]/s[i]) option2 <- x %*% diag(1/s) option3 <- sweep(x, 2, s, '/') all.equal(option1,option2)
[1] TRUE
all.equal(option2,option3)
[1] TRUE
microbenchmark(sapply(1:2, function(i) x[,i]/s[i]), x %*% diag(1/s), sweep(x, 2, s, '/'))
Unit: microseconds
expr min lq mean median uq max neval
sapply(1:2, function(i) x[, i]/s[i]) 18.425 19.8800 37.42147 21.5765 22.789 1583.518 100
x %*% diag(1/s) 1.456 2.1830 2.86150 2.9100 3.395 9.213 100
sweep(x, 2, s, "/") 32.486 34.1825 37.31964 35.3950 36.364 131.395 100
From: Eric Berger <ericjberger at gmail.com>
Sent: Wednesday, March 3, 2021 3:19 PM
To: Harold Doran <harold.doran at cambiumassessment.com>
Cc: Steven Yen <styen at ntu.edu.tw>; R-help Mailing List <r-help at r-project.org>
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 <harold.doran at cambiumassessment.com<mailto:harold.doran at cambiumassessment.com>> wrote:
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 <r-help-bounces at r-project.org<mailto:r-help-bounces at r-project.org>> On Behalf Of Steven Yen
Sent: Wednesday, March 3, 2021 6:00 AM
To: R-help Mailing List <r-help at r-project.org<mailto:r-help at r-project.org>>
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,] 5 15
[6,] 6 16
[7,] 7 17
[8,] 8 18
[9,] 9 19
[10,] 10 20
> s
[1] 1 2
> t(t(x)/s)
[,1] [,2]
[1,] 1 5.5
[2,] 2 6.0
[3,] 3 6.5
[4,] 4 7.0
[5,] 5 7.5
[6,] 6 8.0
[7,] 7 8.5
[8,] 8 9.0
[9,] 9 9.5
[10,] 10 10.0
______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.