Hi guys, I am trying to replace all elements of earth that are equal to zero with their corresponding elements in mars. I can do the replace with a bunch of for-loops, but I don't think this is the R way of doing things. my_list <- list( earth=array(c(0,0,45,0,0,45,0,45),dim=c(2,2,2)), mars=array(c(8:1),dim=c(2,2,2))) my_list for (i in c(1:2)) { for (j in c(1:2)) { for (k in c(1:2)) { if (my_list$earth[i,j,k] == 0) { my_list$earth[i,j,k] <- my_list$mars[i,j,k] } } } } my_list Do you guys have any suggestions for getting rid of the ugly for-loops? Many thanks, Fernando Álvarez Nordea e-Markets Strandgade 3 PO Box 850 DK-0900 Copenhagen C Denmark Tel.: +45 33 33 32 67 Mobile: +45 61 55 27 54 This transmission is intended solely for the person or entity to whom it is addressed. It may contain privileged and confidential information. If you are not the intended recipient, please be notified that any dissemination, distribution or copying is strictly prohibited. If you have received this transmission by mistake, please let us know and then delete it from your system. P Please consider the impact on the environment before printing this e-mail. [[alternative HTML version deleted]]
On Tue, Sep 27, 2011 at 9:43 AM, R. Michael Weylandt < michael.weylandt@gmail.com> wrote:> Untested, I believe this should work, though you might need to modify for > floating point funny business in testing the equalities: > > my_list <- list( earth=array(c(0,0,45,0,0,45,0,45),dim=c(2,2,2)), > mars=array(c(8:1),dim=c(2,2,2))) > my_list$earth[my_list$earth==0] <- my_list$mars[my_list$earth==0] > > Hope this helps, > > Michael Weylandt > > On Tue, Sep 27, 2011 at 8:49 AM, <fernando.cabrera@nordea.com> wrote: > >> Hi guys, >> >> I am trying to replace all elements of earth that are equal to zero with >> their corresponding elements in mars. I can do the replace with a bunch of >> for-loops, but I don't think this is the R way of doing things. >> >> my_list <- list( earth=array(c(0,0,45,0,0,45,0,45),dim=c(2,2,2)), >> mars=array(c(8:1),dim=c(2,2,2))) >> my_list >> for (i in c(1:2)) { >> for (j in c(1:2)) { >> for (k in c(1:2)) { >> if (my_list$earth[i,j,k] =>> 0) { >> >> my_list$earth[i,j,k] <- my_list$mars[i,j,k] >> } >> } >> } >> } >> my_list >> >> Do you guys have any suggestions for getting rid of the ugly for-loops? >> >> Many thanks, >> >> Fernando Álvarez >> >> Nordea e-Markets >> Strandgade 3 >> PO Box 850 >> DK-0900 Copenhagen C >> Denmark >> Tel.: +45 33 33 32 67 >> Mobile: +45 61 55 27 54 >> >> This transmission is intended solely for the person or entity to whom it >> is addressed. It may contain privileged and confidential information. If you >> are not the intended recipient, please be notified that any dissemination, >> distribution or copying is strictly prohibited. If you have received this >> transmission by mistake, please let us know and then delete it from your >> system. >> >> P Please consider the impact on the environment before printing this >> e-mail. >> >> >> [[alternative HTML version deleted]] >> >> >> ______________________________________________ >> R-help@r-project.org mailing list >> 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]]
Thanks Michael it works! Have to say it is amazing what you can do in R with a few lines (a line in this case) of code. Fernando From: R. Michael Weylandt [mailto:michael.weylandt@gmail.com] Sent: 27. september 2011 15:43 To: Cabrera, Fernando Álvarez Subject: Re: [R] Matrix and list indices Untested, I believe this should work, though you might need to modify for floating point funny business in testing the equalities: my_list <- list( earth=array(c(0,0,45,0,0,45,0,45),dim=c(2,2,2)), mars=array(c(8:1),dim=c(2,2,2))) my_list$earth[my_list$earth==0] <- my_list$mars[my_list$earth==0] Hope this helps, Michael Weylandt On Tue, Sep 27, 2011 at 8:49 AM, <fernando.cabrera@nordea.com<mailto:fernando.cabrera@nordea.com>> wrote: Hi guys, I am trying to replace all elements of earth that are equal to zero with their corresponding elements in mars. I can do the replace with a bunch of for-loops, but I don't think this is the R way of doing things. my_list <- list( earth=array(c(0,0,45,0,0,45,0,45),dim=c(2,2,2)), mars=array(c(8:1),dim=c(2,2,2))) my_list for (i in c(1:2)) { for (j in c(1:2)) { for (k in c(1:2)) { if (my_list$earth[i,j,k] == 0) { my_list$earth[i,j,k] <- my_list$mars[i,j,k] } } } } my_list Do you guys have any suggestions for getting rid of the ugly for-loops? Many thanks, Fernando Álvarez Nordea e-Markets Strandgade 3 PO Box 850 DK-0900 Copenhagen C Denmark Tel.: +45 33 33 32 67<tel:%2B45%2033%2033%2032%2067> Mobile: +45 61 55 27 54<tel:%2B45%2061%2055%2027%2054> This transmission is intended solely for the person or entity to whom it is addressed. It may contain privileged and confidential information. If you are not the intended recipient, please be notified that any dissemination, distribution or copying is strictly prohibited. If you have received this transmission by mistake, please let us know and then delete it from your system. P Please consider the impact on the environment before printing this e-mail. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org<mailto:R-help@r-project.org> mailing list 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]]