[R] error: no such index at level 2

Petr PIKAL petr.pikal at precheza.cz
Thu Jul 9 10:26:14 CEST 2009


Hi

Godmar Back <godmar at gmail.com> napsal dne 08.07.2009 15:28:50:

> On Wed, Jul 8, 2009 at 4:22 AM, Petr PIKAL <petr.pikal at precheza.cz> 
wrote:
> Hi
> 
> r-help-bounces at r-project.org napsal dne 07.07.2009 19:06:17:
> 
> > Hi,
> >
> > I am confused about how to select elements from a list.
> >
> > I'm trying to select all rows of a table 'crossRsorted' such that the
> > mean of a related vector is > 0.  The related vector is accessible as
> > a list element l[[i]] where i is the row index.
> >
> > I thought this would work:
> >
> > > crossRsorted[mean(q[[ crossRsorted[,1] ]], na.rm = TRUE) > 0, ]
> > Error in q[[crossRsorted[, 1]]] : no such index at level 2

> Strange, I got completely different error. Couldn't be that only 
***you***
> have crossRsorted?
> 
> Ok, fair enough. I'm still thinking of a language in which the meaning 
of 
> operators is apparent from their syntactical structure - probably need 
to read
> more of "The R Inferno".
> 
> Here's an example that reproduces the problem, I think (though the error 

> message is slightly different):
> 
> > q<-list()
> > q[[105]] <- as.numeric(c(0,0,1))
> > q[[104]] <- as.numeric(c(1,1,1))
> > q[[10]] <- as.integer(c(3,3,1))
> > crossRsorted <- data.frame(i = c(105, 104,10))
> > q[[ crossRsorted[,1] ]]
> Error in q[[crossRsorted[, 1]]] : recursive indexing failed at level 2

for that 
q[crossRsorted[,1]]

works but your real list is much more complicated.

> 
> Even though the list 'q' has component 105, 104, and 10, the expression 
q[[ 
> crossRsorted[,1] ]] causes an error.
> Why?
> 
> And why does this work:
> 
> > q[[c(105)]]
> [1] 0 0 1
> 
> but not this:
> 
> > q[[c(105,104)]]
> Error in q[[c(105, 104)]] : subscript out of bounds
> > q[[c(105,104,10)]]
> Error in q[[c(105, 104, 10)]] : recursive indexing failed at level 2
> 
> even though q[[105]], q[[104], and q[[10]] are perfectly legitimate 
items?
> 
> Coming back to my question, how to I express "select all i in a vector 
for 
> which q[[i]] meets some predicate, where q is a list?"
> 
> Thank you for the tip about 'str' - that's the typeof function I've been 

> craving. (I thought 'attributes' or 'summary' was all there was...)
> The output for str in the original problem:
> 
> In my original problem, the output is:
> 
> 
> > str(crossRsorted)
> 'data.frame':   15750 obs. of  5 variables:
>  $ i     : num  105 104 9 8 10 9 98 97 10 8 ...
>  $ j     : num  104 105 8 9 9 10 97 98 8 10 ...
>  $ r     : num  -0.973 -0.973 0.764 0.764 0.744 ...
>  $ n     : num  135 135 138 138 138 138 136 136 138 138 ...
>  $ pvalue: num  2.90e-86 2.90e-86 0.00 0.00 0.00 ...
> 
> and
> 
> > str(q)
> List of 165
>  $ : NULL
>  $ : NULL
>  $ : NULL
>  $ : NULL
>  $ :'data.frame':       138 obs. of  1 variable:
>   ..$ howdidyouhear: chr [1:138] "0 3" "3" "3" "3" ...
>  $ :'data.frame':       138 obs. of  1 variable:
>   ..$ approximatelywhendidyoustart: int [1:138] 0 0 5 1 5 5 1 2 6 0 ...
> [ main body deleted ]
>  $ :'data.frame':       138 obs. of  1 variable:
>   ..$ revisiontestpage: num [1:138] 0 0 0 0 0 0 0 0 0 0 ...

and referring to what you wrote to Henrique

This appears to be doing something different. For instance, my 'q' has 165 
components, but what you suggest has 15750:
> length(q)
[1] 165
> length(q[ crossRsorted[,1] ])
[1] 15750

hardly what I want.

but this is what you asked for.

crossRsorted has 15750 items and all has been selected from q.

length(q[ unique(crossRsorted[,1] ]))
could be what you are looking for. However the result is probably a list 
of data frames not a data frame with "rows".

Regards
Petr

> 
> basically - a heterogeneous sparse list of NULL and data.frames of types 

> character, num, and int.
> 
> However - by construction - the q[[i]] for i in crossRsorted[,1] are all 
non-
> NULL, as in my small reproducible example above.

> with data frame and list
> 
> df1[sapply(list1,mean)>0,]
> 
> selects rows of df1 which correspond to list elements with mean >0
> 
> I can't run 'sapply' over my list because sapply will also iterate over 
the 
> NULLs. I want to access only those components in list1 that occur in 
df1[1,].
> 
>  - Godmar
>




More information about the R-help mailing list