[R] Expand a data frame

arun smartpink111 at yahoo.com
Mon Dec 30 02:43:32 CET 2013


HI,
May be this helps:
dat1<- read.table(text="date event
01-jan-04 4
02-jan-04 3
03-jan-04 2
04-jan-04 3
05-jan-04 2
06-jan-04 2
07-jan-04 4",sep="",header=TRUE,stringsAsFactors=FALSE) 
 vec1 <- rep(1:nrow(dat1),dat1$event)
res <- transform(dat1[vec1,],event=1,id=seq(length(vec1)))
row.names(res) <- 1:nrow(res)


A.K.


I have an event per day which I wish to expand .  As an output I wish to have a long data format  with a unique sequential id for all events.   
Sample data and what I desire to have is shown below 


Current stand 

date	event 
01-jan-04	4 
02-jan-04	3 
03-jan-04	2 
04-jan-04	3 
05-jan-04	2 
06-jan-04	2 
07-jan-04	4 
                

My desired output for the first two rows 
                
date	event	id 
01-jan-04	1	1 
01-jan-04	1	2 
01-jan-04	1	3 
01-jan-04	1	4 
02-jan-04	1	5 
02-jan-04	1	6 
02-jan-04	1	7



More information about the R-help mailing list