[R] Dividing objects in classes using function sample()

Gabor Grothendieck ggrothendieck at gmail.com
Sat Sep 2 18:38:26 CEST 2006


If n and k are small try brute force.

g <- expand.grid(0:4, 0:4, 0:4)
g[rowSums(g) == 4,]

or more generally:

n <- 4; k <- 3
g <- do.call(expand.grid, rep(list(0:n), k))
g[rowSums(g) == n,]

On 9/2/06, "Maja Schröter" <maja.schroeter at gmx.de> wrote:
> Hello everyone,
>
> I've a problem and dont know how to solve. This is my first posting and it would be fantastic if you could help me.
>
> I want to divide n objects in k classes and need an output with all (n+1)(n+2)/2 possibilities.
>
> For example n=4, k=3:
>
> That would be:
>
>   4 0 0
>   3 1 0
>   3 0 1
>   2 2 0
>   2 1 1
>   2 0 2
>   1 3 0
>   1 2 1
>   1 1 2
>   1 0 3
>   0 3 1
>   0 2 2
>   0 1 3
>   0 0 4
>
>
> I tried to you use permn() or sample() but I don't know how to solve.
>
> Please help me!
>
> Best regards,
>
> Maja
>
>
> --
>
>
> "Feel free" – 10 GB Mailbox, 100 FreeSMS/Monat ...
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list