Abrahams, Mark
2006-Oct-24 12:35 UTC
[R] incorrect number of subscriptions error in user-written function
I have written a function which returns an "incorrect number of
subscriptions error" and am unable to identify why this should happen.
Any help would be gratefully received.
The problem:
I would like to generate combinations/permutations across different
selections - for example all the combinations of "3 chosen from 6"
combined with "5 chosen from 8" combined with "2 chosen from
4".
Loading gtools I can generate the combinations for each selection
sec1 = combinations(4,2,1:4)
sec2 = combinations(6,3,5:10)
sec3 = combinations(8,5,11:18)
I then wrote a function to combine the combinations for each selection.
multiperm <-function(x, y) # function to combine combinations
{
xdim = nrow(x)
xcol = ncol(x)
ydim = nrow(y)
ycol = ncol(y)
v1 = matrix( nrow = xdim*ydim, ncol = xcol+ycol )
for ( i in 1:xdim) {
for (j in 1:ydim){
v1[i + (j-1)*xdim , ] = c(x[i,], y[j, ])}
}
v1 = data.frame(v1) # set output as dataframe
}
This works fine when combining sec1 and sec2,
comb = multiperm(sec1,sec2)
However when I try to reapply the function to combine the joint
combinations of sec1 and sec2 along with sec3
comb= data.frame(comb)
acomb = multiperm(comb,sec3)
an "Error: incorrect number of subscripts on matrix" error occurs.
Can anyone explain why this is the case and how I can resolve it?
Many thanks
Mark Abrahams
We provide assessment products and services that enable organisations to
recruit, retain and develop talented people.
[[alternative HTML version deleted]]
