Question

I have one dataset consists start_date, end_date and var_string. Here I want to create new dataset consists individual dates into 'DATE' starting from start_date to end_date, and 'var_string' should contain repeated string as follows start and end date. Here is my sample data:

start_date  end_date    var_string

1/2/2012    5/2/2012    Apple

6/2/2012    4/3/2012    Sony

6/2/2012    4/3/2012    Dell

so I want to transform this dataset in R as follows:

Date        var_string

1/2/2012    Apple

2/2/2012    Apple

3/2/2012    Apple

4/2/2012    Apple

5/2/2012    Apple

6/2/2012    Sony

.           .

.           .

.           .

4/3/2012    sony

6/2/2012    Dell

.           .

.           .

.           .
4/3/2012    Dell

and so on..

So please help me in getting new dataset in R as mentioned above.

Was it helpful?

Solution

I tried and get the output using the following small program, but I need it in more effective way in small lines of R code. Anyone could help me.

temp=data.frame()

nds1=data.frame()

bn=data.frame()

for(i in 1:nrow(data))

{

nds=rbind(data.frame("Date"=seq(as.Date(data$Start.Date[i], "%d-%m-%Y"),

as.Date(data$End.Date[i], "%d-%m-%Y"),by="days")))

nds1=rbind(nds1, nds)

temp=rbind(temp,nrow(nds))

bn=rbind(bn,data.frame("Promo.Type"=rep(data$Promo.Type[i],temp[i,1])))

}

final=data.frame(nds1, bn)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top