An Eager Avocado

Eager Avocado

I give myself very good advice, but I very seldom follow it.

Recursive indexing failed

,

An example where recursive indexing failed error is not clear

mydf = list(a=c(1,2,3,5,7,11,13),
            b=c(1,3,5,7,9,11,13),
            c=c(2,4,6,8,10,12,14))

Indexing of mydf is done by list indexing syntax

mydf$a
## [1]  1  2  3  5  7 11 13
mydf[['b']]
## [1]  1  3  5  7  9 11 13

However, inside an outer function, it does not work

k = names(mydf)
myfunc = function(x,y) {
    return(length(mydf[[x]]) + length(mydf[[y]]))
}

tryCatch(expr = {
    outer(k,k,myfunc)
    }, error= function(e) {
        print(e)
})
## <simpleError in mydf[[x]]: recursive indexing failed at level 2
## >

Similarly with sapply

tryCatch(expr = {
    sapply(k,function(i) {return(mydf[[k]][1])})
    }, error = function(e) {
        print(e)
})
## <simpleError in mydf[[k]]: recursive indexing failed at level 2
## >

Possible explanation is that these vectorized function used an underlying vectorized indexing, for example sapply(avector, function(i) { return blist[[i]]}) will try to vectorize anything in place of i, that is, the call inside the function will become blist[[avector]], which is list indexing syntax and would not work with vector indices.