[R] nesting same set of values with each level of another set of values
Dimitri Liakhovitski
dimitri.liakhovitski at gmail.com
Fri Mar 11 20:03:26 CET 2011
Thanks a lot, David!
Exactly what I was looking for!
Dimitri
On Fri, Mar 11, 2011 at 1:46 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Mar 11, 2011, at 1:26 PM, Dimitri Liakhovitski wrote:
>
>> 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)
>>
>
> You might want to see if this is more "R-like:
>
> myframe <- expand.grid(unit=units, Weeks=mydates)
>
> (It will probably be a lot faster if you need to "scale up" this operation.)
>
> --
> David.
>>
>> ### 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
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
--
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com
More information about the R-help
mailing list