From hadley at rice.edu  Fri Jan  6 19:31:55 2012
From: hadley at rice.edu (Hadley Wickham)
Date: Fri, 6 Jan 2012 12:31:55 -0600
Subject: [Rd] seq_along and rep_along
Message-ID: <CABdHhvEUtWfj6+2kT6fsD5CYgx66g1+C5gNhdLTA0M60Dw+YbQ@mail.gmail.com>

Hi all,

A couple of ideas for improving seq_along:

* It would be really useful to have a second argument dim:

    seq_along(mtcars, 1)
    seq_along(mtcars, 2)
    # equivalent to
    seq_len(dim(mtcars)[1])
    seq_len(dim(mtcars)[2])

  I often find myself wanting to iterate over the rows or column of a
data frame, and there isn't a particularly nice idiom if you want to
avoid problems with zeros - you have to use seq_len(nrow(df)) etc

* To me, it would seem be very natural to have a rep_along function:

  rep_along <- function(x, y) rep(x, length.out = length(y))

  possibly with more checking for the case where the lengths aren't
integer multiples.

I'd be happy to submit proposed implementations/documentation if there
was interest.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] seq_along and rep_along
Hadley Wickham