Skip to content
Back to formatted view

Raw Message

Message-ID: <bc80bd3b-3925-d755-0b37-f290e9bec4e8@ntu.edu.tw>
Date: 2021-03-03T11:40:17Z
From: Steven Yen
Subject: Column-by-column division
In-Reply-To: <f880a902-b1ff-4f21-e63c-37135c0160ac@sapo.pt>

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.