[R] nesting same set of values with each level of another set of values

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri Mar 11 19:26:56 CET 2011


Hello!

The code below works and does what I need it to do.
However, I think the way I create myframe (last step) is very
un-R-like and probably not very efficient. I am sure there are better,
more R-appropriate methods.
Any pointers?
Thanks a lot!
Dimitri

# Step 1: Creating a vector of dates:
mydates<-seq(as.Date("2008-12-29"), length = 50, by = "week")
(mydates)

# Step 2: Creating a vector of units:
units<-1:20
(units)

### Step3: Creating a data frame with 2 columns: all units stacked
(col 1) with all dates in each (col 2):
myframe<-NULL
for(i in 1:length(units)){  # i<-1
  myframe[[i]]<-data.frame(unit=rep(units[i],length(mydates)),Weeks=mydates)
}
myframe<-do.call(rbind,myframe)
str(myframe)
head(myframe,110)


-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list