[R] using contents of one column to direct addition of new rows in dataframe

Jessi Brown jessilbrown at gmail.com
Mon Nov 23 21:32:53 CET 2009


Hello, R gurus. I've been trying to get R to do some data manipulation
for me, and so far have been stumped in figuring out any elegant way
to do so. Searches through the R-help archive haven't helped, so now
I'm trying plan B.

Suppose I have a dataframe that summarizes events that took place
between some interval of time in days, "Time." I want to expand the
dataframe to repeat each row the appropriate number of days so that
Time is now = 1 for each original record. Actually, I really don't
care if Time=1 is displayed in the new dataframe, as it will no longer
be relevant.

Also, I want to have R keep track of a time-variant variable, Date, so
that it advances along one day for each new row. Here's an example of
what the starting record would be like, and what I'd like to end up
with:

> foo.df <- data.frame(ID="N1", Date=2, Time=3)
> foo.df
  ID Date Time
1 N1    2    3

# insert R wizardry here to get new.foo.df

>new.foo.df
ID	Date	Time
N1	2	1
N1	3	1
N1	4	1


I know, or at least suspect, that there is a fairly straightforward
way of doing so with building some ugly loop. But even after
considering versions of "apply" and related commands, I just can't
figure it out. Any help would be greatly appreciated.

cheers, Jessi Brown

University of Nevada, Reno




More information about the R-help mailing list