Re: [R] Confusing fori or ifelse result in matrix manipulation
Hello, If !!x obfuscates what the is doing, there's M[, as.logical(x)] <- 0 Hope this helps, Rui Barradas Às 15:52 de 25/04/2022, Ivan Calandra escreveu: Indeed, of course, my bad! I got confused by Bert's answer... So my first suggestion to do M[, x == 1] was correct :) -- 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:44, Eric Berger wrote: M[,x] <- 0 *does not do the same* !! even if x only contains 0's and 1's To understand this, note that M[,c(2,3)] gives columns 2 and 3 M[,c(0,3)] gives the 3rd column and M[,c(0,1,0)] gives the *first* column!! On Mon, Apr 25, 2022 at 5:41 PM Ivan Calandra wrote: 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 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 On Behalf Of Uwe Freier >>> Sent: Sunday, April 24, 2022 11:06 AM >>> To: r-help@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 >>> >>> __ >>>
Re: [R] Confusing fori or ifelse result in matrix manipulation
Oh, I get it. The context was in choosing columns to set to 0 via a **predicate**. Sorry for the noise. Bert On Mon, Apr 25, 2022 at 7:39 AM Bert Gunter wrote: > > Yes, sorry. But it's with the logical cast it's still M[, c(FALSE, > TRUE, FALSE)] which is M[, 2], and so I still don't get the point. > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > On Mon, Apr 25, 2022 at 7:33 AM Eric Berger wrote: > > > > M[,x==1] is not the same as M[,x] :-) > > However, M[,!!x] is the same as M[,x==1] and saves one character! > > The point of this is "I can name that tune in ... " (as if that was not > > obvious) > > > > > > > > On Mon, Apr 25, 2022 at 5:30 PM 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 > >> 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 On Behalf Of Uwe Freier > >> > > Sent: Sunday, April 24, 2022 11:06 AM > >> > > To: r-help@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,]107 > >> > > [2,]208 > >> > > [3,]309 > >> > > > >> > > > >> > > but the result is: > >> > > > >> > >> M > >> > >[,1] [,2] [,3] > >> > > [1,]107 > >> > > [2,]107 > >> > > [3,]107 > >> > > > >> > > > >> > > 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= > >> > > PLEASE do read the posting guide > >> > > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= > >> > > and provide commented, minimal, self-contained, reproducible code. > >> > > > >> > > __ > >> > >
Re: [R] Confusing fori or ifelse result in matrix manipulation
Yes, sorry. But it's with the logical cast it's still M[, c(FALSE, TRUE, FALSE)] which is M[, 2], and so I still don't get the point. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Apr 25, 2022 at 7:33 AM Eric Berger wrote: > > M[,x==1] is not the same as M[,x] :-) > However, M[,!!x] is the same as M[,x==1] and saves one character! > The point of this is "I can name that tune in ... " (as if that was not > obvious) > > > > On Mon, Apr 25, 2022 at 5:30 PM 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 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 On Behalf Of Uwe Freier >> > > Sent: Sunday, April 24, 2022 11:06 AM >> > > To: r-help@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,]107 >> > > [2,]208 >> > > [3,]309 >> > > >> > > >> > > but the result is: >> > > >> > >> M >> > >[,1] [,2] [,3] >> > > [1,]107 >> > > [2,]107 >> > > [3,]107 >> > > >> > > >> > > 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= >> > > PLEASE do read the posting guide >> > > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= >> > > and provide commented, minimal, self-contained, reproducible code. >> > > >> > > __ >> > > R-help@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@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read
Re: [R] Confusing fori or ifelse result in matrix manipulation
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 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 On Behalf Of Uwe Freier Sent: Sunday, April 24, 2022 11:06 AM To: r-help@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,]107 [2,]208 [3,]309 but the result is: M [,1] [,2] [,3] [1,]107 [2,]107 [3,]107 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= and provide commented, minimal, self-contained, reproducible code. __ R-help@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@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@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.
Re: [R] Confusing fori or ifelse result in matrix manipulation
M[,x==1] is not the same as M[,x] :-) However, M[,!!x] is the same as M[,x==1] and saves one character! The point of this is "I can name that tune in ... " (as if that was not obvious) On Mon, Apr 25, 2022 at 5:30 PM 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 > 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 On Behalf Of Uwe Freier > > > Sent: Sunday, April 24, 2022 11:06 AM > > > To: r-help@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,]107 > > > [2,]208 > > > [3,]309 > > > > > > > > > but the result is: > > > > > >> M > > >[,1] [,2] [,3] > > > [1,]107 > > > [2,]107 > > > [3,]107 > > > > > > > > > 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= > > > PLEASE do read the posting guide > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > __ > > > R-help@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@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@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. > [[alternative HTML version deleted]]
Re: [R] Confusing fori or ifelse result in matrix manipulation
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 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 On Behalf Of Uwe Freier > > Sent: Sunday, April 24, 2022 11:06 AM > > To: r-help@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,]107 > > [2,]208 > > [3,]309 > > > > > > but the result is: > > > >> M > >[,1] [,2] [,3] > > [1,]107 > > [2,]107 > > [3,]107 > > > > > > 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= > > PLEASE do read the posting guide > > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= > > and provide commented, minimal, self-contained, reproducible code. > > > > __ > > R-help@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@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@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.
Re: [R] Confusing fori or ifelse result in matrix manipulation
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 On Behalf Of Uwe Freier Sent: Sunday, April 24, 2022 11:06 AM To: r-help@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,]107 [2,]208 [3,]309 but the result is: M [,1] [,2] [,3] [1,]107 [2,]107 [3,]107 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= and provide commented, minimal, self-contained, reproducible code. __ R-help@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@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.
Re: [R] Confusing fori or ifelse result in matrix manipulation
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 On Behalf Of Uwe Freier Sent: Sunday, April 24, 2022 11:06 AM To: r-help@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,]107 [2,]208 [3,]309 but the result is: > M [,1] [,2] [,3] [1,]107 [2,]107 [3,]107 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@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=K2RWPvtxaxwigGGH2oOrg8qiDWC5KTu60b8Wjybwsg4= PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=eyJm06tVDfKvtMDgz6oIWM-WVdoW3Szzb5G6rq0cCO_cB6ljj2x80E4oRkt3Vgba=L9VXAAYzIzrG2h17hBO-Qfg_EoS2mRQbjs3sRESp62Q= and provide commented, minimal, self-contained, reproducible code. __ R-help@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.
Re: [R] Confusing fori or ifelse result in matrix manipulation
В Sun, 24 Apr 2022 17:05:55 +0200 Uwe Freier пишет: > ifelse(x[i] == 0, A[,i], 0) Hint: what does ifelse return instead of a vector of length nrow(A)? Since you're checking conditions of length 1, you can safely use `if (x[i] == 0) A[,i] else 0` here, or you can transform the `x` vector into a boolean matrix of the correct shape to guide the substitution: X <- matrix(as.logical(x), nrow(A), ncol(A), byrow = TRUE) ifelse(X, 0, A) -- Best regards, Ivan __ R-help@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.