Skip to content

subloop - flexible coding - variable number of loops

2 messages · Peter Jaksons, David Winsemius

#
On Jan 27, 2011, at 6:29 PM, Peter Jaksons wrote:

            
Have you done any thinking about the size of hte programming task?  
Perhaps running this code?

 > 20^(1:10)
  [1] 2.000e+01 4.000e+02 8.000e+03 1.600e+05 3.200e+06 6.400e+07  
1.280e+09 2.560e+10 5.120e+11 1.024e+13

So you might want to think twice about trying for n > 6 right off the  
bat/

expand.grid could be used:

col3 <- expand.grid(as.data.frame(matrix(rep(1:20, 3), ncol=3)))
col3[apply(col3, 1,sum)==20, ]

 > nrow( col3[apply(col3, 1,sum)==20, ] )
[1] 171