# [R] Confusing fori or ifelse result in matrix manipulation

Ivan Calandra |v@n@c@|@ndr@ @end|ng |rom rgzm@de
Mon Apr 25 16:38:25 CEST 2022

```Indeed, M[, x] <- 0 does the same, but only if x is 0's and 1's only,
right? I thought that it might not always be the case so I choose this
maybe superflous approach.

M[, 2] does the same of course in the example, but I was assuming that
the columns to change to zero are not known in advance and are based on
data contained in another vector. Where that vector comes from is
important too because the whole thing might be unnecessary. Is that
maybe what you were hinting at, Bert?

Maybe Uwe can tell us more about what/why he wants to do!

Ivan

--
Dr. Ivan Calandra
Imaging lab
RGZM - MONREPOS Archaeological Research Centre
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

On 25/04/2022 16:30, Bert Gunter wrote:
> x == 1 is the same as M[, x] so your expression is the same as
> M[, c(FALSE, TRUE, FALSE)] <- 0
> which is the same as M[, 2]  <- 0
>
> So what is the point of all this, exactly?
>
> Bert
>
> On Mon, Apr 25, 2022 at 7:18 AM Ivan Calandra <ivan.calandra using rgzm.de> wrote:
>> Hi Uwe,
>>
>> If I understood the problem completely and building up on Tim's answer,
>> this is even easier:
>> M <- A <- matrix(1:9, ncol = 3)
>> x <- c(0, 1, 0)
>> M[, x == 1] <- 0
>> M
>>
>> The original issue was with the way ifelse works. The explanation is in
>> the help page: "ifelse returns a value with the same shape as test||".
>> So, because x[i] == 0 returns a single value (TRUE or FALSE), ifelse
>> will also return a single value (either A[, i][1] or 0) and not a vector
>> of length 3 as you wanted. This single value is recycled to fill M[, i],
>> hence the result.
>>
>> HTH,
>> Ivan
>>
>> --
>> Dr. Ivan Calandra
>> Imaging lab
>> RGZM - MONREPOS Archaeological Research Centre
>> Schloss Monrepos
>> 56567 Neuwied, Germany
>> +49 (0) 2631 9772-243
>> https://www.researchgate.net/profile/Ivan_Calandra
>>
>> On 25/04/2022 16:01, Ebert,Timothy Aaron wrote:
>>> A <- matrix(1:9,ncol=3)
>>> x <- c(0,1,0)
>>> M <- matrix(ncol=3,nrow=3)
>>> M<-A
>>> for(i in 1:3) {
>>>     if(x[i]){
>>>       M[,i] <-0
>>>       }
>>>     }
>>> }
>>> M
>>>
>>> The outcome you want is to set all of the middle column values to zero. So I used x as a logical in an if test and when true everything in that column is set to zero.
>>>
>>> Your approach also works but you must go through each element explicitly.
>>> A <- matrix(1:9,ncol=3)
>>> x <- c(0,1,0)
>>> M <- matrix(ncol=3,nrow=3)
>>> for(j in 1:3){
>>>     for(i in 1:3){
>>>       ifelse(x[i]==1, M[j,i]<-0, M[j,i]<-A[j,i])
>>>     }
>>> }
>>> M
>>>
>>>
>>>
>>> Tim
>>>
>>> -----Original Message-----
>>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Uwe Freier
>>> Sent: Sunday, April 24, 2022 11:06 AM
>>> To: r-help using r-project.org
>>> Subject: [R] Confusing fori or ifelse result in matrix manipulation
>>>
>>> [External Email]
>>>
>>> Hello,
>>>
>>> sorry for the newbie question but I can't find out where I'm wrong.
>>>
>>> A <- matrix(1:9,ncol=3)
>>> x <- c(0,1,0)
>>> M <- matrix(ncol=3,nrow=3)
>>> for(i in 1:3) {
>>>     M[,i] <- ifelse(x[i] == 0, A[,i], 0)
>>> }
>>>
>>> expected:
>>>
>>>> M
>>>         [,1] [,2] [,3]
>>> [1,]    1    0    7
>>> [2,]    2    0    8
>>> [3,]    3    0    9
>>>
>>>
>>> but the result is:
>>>
>>>> M
>>>         [,1] [,2] [,3]
>>> [1,]    1    0    7
>>> [2,]    1    0    7
>>> [3,]    1    0    7
>>>
>>>
>>> If I do it "manually":
>>>
>>>> M[,1] <- A[,1]
>>>> M[,2] <- 0
>>>> M[,3] <- A[,3]
>>> M is as expected, where is my misconception?
>>>
>>> Thanks for any hint and best regards,
>>>
>>> Uwe
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba&s=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4&e=
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help