Hi Kehl,
How large are n and k in your case? Using Dimitris' approach and I got the
following timings for 1000 replicates:
# function based on Dimitri's reply
foo <- function(n, k){
r <- expand.grid(rep(list(0:n), k))
subset(r, rowSums(r) == n)
}
# a second try
foo2 <- function(n, k){
r <- expand.grid(rep(list(0:n), k))
r[rowSums(r) == n, ]
}
# n = 6, k = 3
> system.time(replicate(1000, foo(6, 3)))
user system elapsed
1.336 0.015 1.345
> system.time(replicate(1000, foo2(6, 3)))
user system elapsed
1.210 0.008 1.209
HTH,
Jorge
> R.version
_
platform x86_64-apple-darwin9.8.0
arch x86_64
os darwin9.8.0
system x86_64, darwin9.8.0
status Patched
major 2
minor 13.0
year 2011
month 04
day 18
svn rev 55504
language R
version.string R version 2.13.0 Patched (2011-04-18 r55504)
On Thu, Apr 21, 2011 at 3:52 PM, Kehl Dániel <> wrote:
> Thank you.
> I only need those where the rowsum = n.
> I could choose those with code, but I dont think it is efficient that way.
>
> daniel
>
> 2011-04-21 12:33 keltezéssel, Dimitris Rizopoulos írta:
>
>
>> expand.grid(rep(list(0:6), 3))
>>
>
> ______________________________________________
> [email protected] 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.
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.