Dear listers, I'm having a bad R day. I just can't think of the vectorized equivalent of: for (ii in 1:n) aa[ii] = bb[ii,cc[ii]] Any suggestion received with embarrassment and gratitude Simon Gatehouse CSIRO Exploration and Mining, Newbigin Close off Julius Ave North Ryde, NSW Mail: PO Box 136, North Ryde NSW 1670, Australia Phone: 61 (2) 9490 8677 Fax: 61 (2) 9490 8921 Mobile: 61 0407 130 635 E-mail: simon.gatehouse at csiro.au Web Page: http://www.csiro.au/
Simon.Gatehouse at csiro.au wrote:> Dear listers, > I'm having a bad R day. I just can't think of the vectorized equivalent of: > > for (ii in 1:n) aa[ii] = bb[ii,cc[ii]] > > Any suggestion received with embarrassment and gratitudea <- b[cbind(1:n, cc[1:n])] Uwe Ligges> Simon Gatehouse > CSIRO Exploration and Mining, > Newbigin Close off Julius Ave > North Ryde, NSW > > Mail: PO Box 136, North Ryde > NSW 1670, Australia > Phone: 61 (2) 9490 8677 > Fax: 61 (2) 9490 8921 > Mobile: 61 0407 130 635 > E-mail: simon.gatehouse at csiro.au > Web Page: http://www.csiro.au/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Wed, 2 Apr 2003 Simon.Gatehouse at csiro.au wrote:> Dear listers, > I'm having a bad R day. I just can't think of the vectorized equivalent of: > > for (ii in 1:n) aa[ii] = bb[ii,cc[ii]]aa <- bb[cbind(1:n, cc[1:n])] -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
What about: bb[cbind(1:length(cc),cc)] Eric At 17:15 2/04/2003 +1000, Simon.Gatehouse at csiro.au wrote:>Dear listers, >I'm having a bad R day. I just can't think of the vectorized equivalent of: > >for (ii in 1:n) aa[ii] = bb[ii,cc[ii]] > >Any suggestion received with embarrassment and gratitude > >Simon Gatehouse >CSIRO Exploration and Mining, >Newbigin Close off Julius Ave >North Ryde, NSW > >Mail: PO Box 136, North Ryde > NSW 1670, Australia >Phone: 61 (2) 9490 8677 >Fax: 61 (2) 9490 8921 >Mobile: 61 0407 130 635 >E-mail: simon.gatehouse at csiro.au >Web Page: http://www.csiro.au/ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help-------------------------------------------------- L'erreur est certes humaine, mais un vrai d?sastre n?cessite un ou deux ordinateurs. Citation anonyme -------------------------------------------------- Eric Lecoutre Informaticien/Statisticien Institut de Statistique / UCL TEL (+32)(0)10473050 lecoutre at stat.ucl.ac.be URL http://www.stat.ucl.ac.be/ISpersonnel/lecoutre --------------------------------------------------
You could calculate the index vector like ind <- n * (cc - 1) + (1:n) and then use it to index bb as a vector aa <- bb[ind] Reid Huntsinger -----Original Message----- From: Simon.Gatehouse at csiro.au [mailto:Simon.Gatehouse at csiro.au] Sent: Wednesday, April 02, 2003 2:15 AM To: r-help at stat.math.ethz.ch Subject: [R] vectorize an expression Dear listers, I'm having a bad R day. I just can't think of the vectorized equivalent of: for (ii in 1:n) aa[ii] = bb[ii,cc[ii]] Any suggestion received with embarrassment and gratitude Simon Gatehouse CSIRO Exploration and Mining, Newbigin Close off Julius Ave North Ryde, NSW Mail: PO Box 136, North Ryde NSW 1670, Australia Phone: 61 (2) 9490 8677 Fax: 61 (2) 9490 8921 Mobile: 61 0407 130 635 E-mail: simon.gatehouse at csiro.au Web Page: http://www.csiro.au/ ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ------------------------------------------------------------------------------