On Wed, Jul 8, 2009 at 4:22 AM, Petr PIKAL <petr.pi...@precheza.cz> wrote:
> Hi > > r-help-boun...@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 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 ... 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 [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.