[R] Create Data Frame From Loop

arun smartpink111 at yahoo.com
Thu Aug 15 16:50:37 CEST 2013


Hi,
You could try:
 res<-data.frame(lapply(wkend,function(x) seq(x,as.Date("2013-12-26"),by=7)))
  colnames(res)<- paste0("date",1:3)
head(res)
#       date1      date2      date3
#1 2013-01-04 2013-01-05 2013-01-06
#2 2013-01-11 2013-01-12 2013-01-13
#3 2013-01-18 2013-01-19 2013-01-20
#4 2013-01-25 2013-01-26 2013-01-27
#5 2013-02-01 2013-02-02 2013-02-03
#6 2013-02-08 2013-02-09 2013-02-10
A.K.



How do I create a data frame from the loop? 

wkend<-as.Date(c("2013-01-04","2013-01-05","2013-01-06"),"%Y-%m-%d") 
while(wkend < "2013-12-27") { 
        wkend <- wkend + 7 
        print(wkend) 
} 

[1] "2013-01-11" "2013-01-12" "2013-01-13" 
[1] "2013-01-18" "2013-01-19" "2013-01-20" 
[1] "2013-01-25" "2013-01-26" "2013-01-27" 
[1] "2013-02-01" "2013-02-02" "2013-02-03" 
[1] "2013-02-08" "2013-02-09" "2013-02-10" 
[1] "2013-02-15" "2013-02-16" "2013-02-17" 
[1] "2013-02-22" "2013-02-23" "2013-02-24" 
[1] "2013-03-01" "2013-03-02" "2013-03-03" 
...... 
[1] "2013-11-15" "2013-11-16" "2013-11-17" 
[1] "2013-11-22" "2013-11-23" "2013-11-24" 
[1] "2013-11-29" "2013-11-30" "2013-12-01" 
[1] "2013-12-06" "2013-12-07" "2013-12-08" 
[1] "2013-12-13" "2013-12-14" "2013-12-15" 
[1] "2013-12-20" "2013-12-21" "2013-12-22" 
[1] "2013-12-27" "2013-12-28" "2013-12-29"



More information about the R-help mailing list