[R] Adding a time variable prior to using reshape

William Dunlap wdunlap at tibco.com
Wed Oct 6 19:06:59 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of peter dalgaard
> Sent: Wednesday, October 06, 2010 7:22 AM
> To: Henrique Dallazuanna
> Cc: r-help at r-project.org; Andre Easom
> Subject: Re: [R] Adding a time variable prior to using reshape
> 
> 
> On Oct 6, 2010, at 16:11 , Henrique Dallazuanna wrote:
> 
> > Try this:
> > 
> > with(foo, ave(Value, Label, FUN = seq)) will generate the 
> time variable
> 
> Make that FUN=seq_along to avoid trouble with groups of size 1.

Sometimes (but not with seq) you also have to worry about
groups of size 0.  It seems a waste and sometimes causes
errors for ave() to call FUN for groups of size 0:

  > trace(seq_along, quote(cat("length(x)=", length(..1), "\n")))
  Tracing function "seq_along" in package "base"
  [1] "seq_along"
  Warning message:
  In .makeTracedFunction(def, tracer, exit, at, print, doEdit) :
    making a traced version of a primitive; arguments will be treated as
'...'
  > ave(101:110, factor(rep(letters[2:4],c(3,3,4)),
levels=letters[1:6]), FUN=seq_along)
  Tracing FUN(X[[1L]], ...) on entry 
  length(x)= 0 
  trace: .prim(...)
  Tracing FUN(X[[2L]], ...) on entry 
  length(x)= 3 
  trace: .prim(...)
  Tracing FUN(X[[3L]], ...) on entry 
  length(x)= 3 
  trace: .prim(...)
  Tracing FUN(X[[4L]], ...) on entry 
  length(x)= 4 
  trace: .prim(...)
  Tracing FUN(X[[5L]], ...) on entry 
  length(x)= 0 
  trace: .prim(...)
  Tracing FUN(X[[6L]], ...) on entry 
  length(x)= 0 
  trace: .prim(...)
  Tracing seq_along(x) on entry 
  length(x)= 10 
  trace: .prim(...)
 [1] 1 2 3 1 2 3 1 2 3 4


Three calls to seq_along(X[[i]]), for i=c(2,3,4),
would suffice.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> 
> -- 
> Peter Dalgaard
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
> 
> ______________________________________________
> R-help at 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.
> 



More information about the R-help mailing list