Hi:
Here's one way; using the mdply() function in the plyr package:
k <- c(1,2,3,4,5)
i <- c(0,1,3,2,1)
# Takes two scalars k and i as input, outputs a data frame
ff <- function(k, i) data.frame(k = rep(k, i+1), i = seq(0, i, by = 1))
library('plyr')
mdply(data.frame(k, i), ff) # returns a data frame
# -------------
Another way to do this is to use the mapply() function
This one returns a matrix.
# -------------
gg <- function(k, i) cbind(k = rep(k, i+1), i = seq(0, i, by = 1))
do.call(rbind, mapply(gg, k, i))
HTH,
Dennis
On Tue, Aug 2, 2011 at 6:40 PM, Kathie <kathryn.lord2000 at gmail.com>
wrote:> Hi, R users,
>
> Here is an example.
>
> k <- c(1,2,3,4,5)
> i <- c(0,1,3,2,1)
>
> if k=1, then i=0
> if k=2, then i=0, 1
> if k=3, then i=0, 1, 2, 3
> if k=4, then i=0, 1, 2
> if k=5, then i=0, 1
>
> so i'd like to create a list like below.
>
>> list
> ? k i
> 1 ?1 0
> 2 ?2 0
> 3 ?2 1
> 4 ?3 0
> 5 ?3 1
> 6 ?3 2
> 7 ?3 3
> 8 ?4 0
> 9 ?4 1
> 10 4 2
> 11 5 0
> 12 5 1
>
> I tried expand.grid, but I can't.
>
> Any suggestion will be greatly appreciated.
>
> Regards,
>
> Kathryn Lord
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/create-a-list-under-constraints-tp3714191p3714191.html
> Sent from the R help mailing list archive at Nabble.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.
>