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

From: Dimitri Liakhovitski <dimitri.liakhovitski_at_gmail.com>
Date: Fri, 11 Mar 2011 14:03:26 -0500

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

______________________________________________
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.
Received on Fri 11 Mar 2011 - 19:07:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Mar 2011 - 19:40:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive