[R] the secret (?) language of lists
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Nov 15 07:28:02 CET 2006
Please, these are NOT lists. They are vectors: the difference should be
clear even from 'An Introduction to R'.
What Peter's first solution does is to create a matrix and then read it
out in the default column-major order. This is again all discussed in
'An Introduction to R'.
Using c() in this way is often frowned on: as.vector() is clearer.
On Tue, 14 Nov 2006, Jeffrey Robert Spies wrote:
> A couple days ago, Mark Leeds asked about a solution that would
> basically stagger two lists, a and b, to return a list in the form of
> a[1], b[1], a[2], b[2], a[3].... In particular, the summary of his
> question was in reference to lists defined by
>
> x <- 5
> tempin <- seq(1,1411, by=30)
> a <- tempin
> b <- tempin + x
>
> I offered the following function
>
> everyOther <- function(tempin, x){
> tempout <- array(data=NA, dim=length(tempin)*2)
> tempout[seq(1,length(tempin)*2, by=2)]<-tempin
> tempout[seq(2,length(tempin)*2, by=2)]<-tempin+x
> tempout
> }
>
> which did what it was supposed to, and Gavin Simpson offered a
> similar function. Peter Dalgaard, however, supplied a much more
> elegant solution:
>
> c(rbind(tempin,tempin+5))
>
> or
>
> rep(tempin, each=2) + c(0,5)
>
> I thought I'd bring this up as a new topic because it's really no
> longer related to what Mark first asked, but is there a way, perhaps
> from the documentation, that a user would know that c() and lists in
> general behave as they do in these two lines? Or would we just need
> to dig into the code?
>
> I am reminded of quote by Byron Ellis: "Contrary to popular belief
> the speed of R's interpreter is rarely the limiting factor to R's
> speed. People treating R like C is typically the limiting factor. You
> have vector operations, USE THEM." Not exactly the point, but close.
>
> Thanks!
>
> Jeff Spies
> http://www.nd.edu/~jspies/
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
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
More information about the R-help
mailing list