Try this:


x <- data.frame(u=c(1:5), seconds=sample(5))
transform(x[rep(1:nrow(x),x$seconds),], seconds =
unlist(lapply(split(x$seconds, x$u), seq)))


On Tue, Jun 9, 2009 at 6:11 PM, Gordon J Holtslander <
gordon.holtslander@usask.ca> wrote:

> I'm new at R ...
> I've not done for loops in R - so this is very new to me.
>
> One of our students has a data frame that contains two columns data
>
> 1. unixtime time of an event (in unix time - #of seconds)
> 2. duration of event in seconds.
>
> We need to create new data - the unixtime (seconds) that these events
> occurred.
>
> We want to create a for loop  (or nested for loops) that goes through the
> first column, and then creates a new unixtime value for each second of
> duration for that event
>
> In a single row of data -
> if the duration was three seconds one iteration of this loop should create
> these values
>
> unixtime
> unixtime = unixtime +1
> unixtime = unixtime +1
> unixtime = unixtime +1
>
> I need these four data values (and all the successive data values created
> by
> the loop(s)) saved into a new dataframe
>
> How do I get R to create a new data object and add data to it on each
> interation of the for loop.
>
> My test for loops only save the data from last iteration of the loop.
>
> Am I missing something really simple?
>
> Any recommended reference on loop and control structures in R?
>
> Thanks,
>
> Gord
> --
> Gordon J. Holtslander
> gordon.holtslander@usask.ca
> Dept. of Biology
> University of Saskatchewan
> tel 306 966-4433
> fax 306 966-4461
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]


