Apply on two similar lists gives two different results

96 Views Asked by At

I created a function to calculate a value called "participation coefficient", and have tested the function with a short list, like so

testList<-c(568120,711503,1077594)
testResults<-(sapply(testList, function(x) participationCoefficient(x)))

Which gives me:

> testResults
               [,1]           [,2]            [,3]
[1,] 568120.0000000 711503.0000000 1077594.0000000
[2,]      0.7333333      0.8780488       0.4166667

Now, when I try to create a list from another source

>authList<-moduleCalcs[[x]]$authId
> authList[1:5]
[1] 548114 553928 553929 556071 559044

And use the apply function

> testResults<-(sapply(authList, function(y) participationCoefficient(y)))

I get

Error in provideDimnames(x) : 
  length of 'dimnames' [1] not equal to array extent
Called from: top level

So I tried to send a shortened authList but I still get the same errors. I am a bit confused as to what is going on here.

Also, here is the code for the function, in case it is necessary

participationCoefficient<-function(auth){
  outmod<-as.data.frame(table(subset(m2$moduleId.x,(m2$A2==auth | m2$A1==auth) & m2$moduleId.y!=m2$moduleId.x)))
  deg<-nrow(subset(m2,m2$A1==auth | m2$A2==auth))
  partcoef<-1-(Reduce("+",(outmod$Freq/deg)))
  answer<-c(auth,partcoef)
  rm(deg,outmod,partcoef)
  return(answer)
}

Any idea on why this would happen?

Edit

Here is some data that can be used for m2. I am not sure if this is the best way to share it. If not, please let me know if there is another way.

authList
[1] 548114 553928

 A2 A1  moduleId.x   moduleId.y
1013122 553928  1   1
1066822 548114  1   1
548114  722690  1   1
548114  666417  1   1
548114  854300  1   1
548114  842554  1   1
548114  991715  1   1
553928  710558  1   1
553928  767591  2   1
553928  718371  1   1
553928  649043  1   1
553928  601167  1   1
553928  637192  2   1
553928  710304  1   1
553928  559044  1   1
563965  553928  1   1
571821  553928  1   1
661623  553928  1   1
682197  553928  1   1
682886  553928  1   1
683583  553928  1   1
712141  553928  1   1
716224  553928  1   1
723022  553928  1   1
851338  553928  1   1
934132  553928  1   1
995296  553928  1   1
1

There are 1 best solutions below

0
On

In this particular instance what was happening was that the subset command here

outmod<-as.data.frame(table(subset(m2$moduleId.x,(m2$A2==auth | m2$A1==auth) & m2$moduleId.y!=m2$moduleId.x)))

was returning an empty set, which was causing problems for the table function. I had to add a conditional statement in the function to safeguard against this possibility.