[R] I need help for creating a "timevar"

Joshua Wiley jwiley.psych at gmail.com
Mon Jul 4 19:55:51 CEST 2011

Hi Karen,

As long as your IDENTITY column goes up in order, this should work:

## example data
dat <- data.frame(IDENTITY = rep(101:103, 3:1), EVENT = "Event")
dat$TIMEVAR <- unlist(with(dat, tapply(EVENT, IDENTITY, seq_along)))
## Result

See ?tapply and ?seq_along for some documentation

Hope this helps,


On Mon, Jul 4, 2011 at 6:41 AM, kbr <kb at flicon.de> wrote:
> Hi all!
> I have data in „Long“ format which I would like to reshape to „Wide“. I know
> that one possibility is the „reshape“ command, which needs a „timevar“.
> Data look as follows: There are approx. 3000 persons („IDENTITY“) and, for
> each person, there are between 2 and 20 events („EVENT“).  For now, there's
> one row for each event (9506 rows)
> http://r.789695.n4.nabble.com/file/n3643658/Screenshot-2.png
> What is missing is the „timevar“ (SPSS calls it „INDEX“), which numbers the
> events WITHIN each person (right column).
> I managed to number the events from 1 to 9506 with the seq-command, first
> writing the number of rows in nEVENT:
>> number <-seq(file=event, 1, nEVENT, b=1)
> Yet, I didn't manage to do so for each individual separately. I guess it
> would be possible with the „split“ command, but I can't figure out how to
> apply it.
> Can anyone give me a hint?
> Thank you!
> Karen
> --
> View this message in context: http://r.789695.n4.nabble.com/I-need-help-for-creating-a-timevar-tp3643658p3643658.html
> Sent from the R help mailing list archive at Nabble.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.

Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles

More information about the R-help mailing list