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

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Fri, 11 Mar 2011 13:46:49 -0500

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 ______________________________________________ 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 - 18:52:25 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:10:21 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