Dear all, Suppose I have a hash created with this x <- list() for (i in c('test', 'some', 'more')){ x[[i]] <- runif(1) } then I want to extract the elem of that hash with a vector> q <- c("some", "more", "not_there")But why this failed?> extracted <- x[[q]]Error in x[[q]] : subscript out of bounds we expect the output of 'extracted' to be a vector as well. When the key is not present to give "NA" in vector - Gundala Viswanath Jakarta - Indonesia
Try this: unlist(ifelse(q %in% names(x), x[q], NA)) On Tue, Jan 13, 2009 at 8:49 AM, Gundala Viswanath <gundalav@gmail.com>wrote:> Dear all, > > Suppose I have a hash created with this > > x <- list() > for (i in c('test', 'some', 'more')){ > x[[i]] <- runif(1) > } > > then I want to extract the elem of that hash with > a vector > > > q <- c("some", "more", "not_there") > > But why this failed? > > > extracted <- x[[q]] > Error in x[[q]] : subscript out of bounds > > we expect the output of 'extracted' to be > a vector as well. When the key is not present > to give "NA" in vector > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Gundala Viswanath wrote:> Dear all, > > Suppose I have a hash created with this > > x <- list() > for (i in c('test', 'some', 'more')){ > x[[i]] <- runif(1) > } >this is not really a hash, even though you can retrieve elements by name, and not only by integer index. also, try to navigate out of the r inferno, see sec 2 in 'the r inferno' (at http://www.burns-stat.com).> then I want to extract the elem of that hash with > a vector > > >> q <- c("some", "more", "not_there") >> > > But why this failed? >?'[[' /Recursive> >> extracted <- x[[q]] >> > Error in x[[q]] : subscript out of bounds > > we expect the output of 'extracted' to be > a vector as well. When the key is not present > to give "NA" in vector >x[[q]] is equivalent to x[['some']][['more']][['not_there']]. since x[['some']] is an atomic integer vector, it won't collaborate with [['more']], and you're done -- since this fails, the recursive indexing fails. on the side, this fails as well (guess why), though some might expect it should work fine: x[[c('some', 1)]] # expected an integer, but subscript out of bounds reported arguably, 'index out of bounds' is not the most enlightening message in cases such as this one: x = 1:10 x[11] # NA, as you might want x[[11]] # d'oh, index out of bounds vQ
Which R version do you have? I'm asking this because my 2.7.0 gives a different error message:> x[[q]]Error in x[[q]] : recursive indexing failed at level 2 Anyway, as Wacek said, x[[q]] is equivalent to x[["some"]][["more"]][["not_there"]] -- and you don't have an element called "more" in x[["some"]]. But you can use [ to do what you want (if the following is what you want).> x[q]$some [1] 0.1118383 $more [1] 0.4524315 $<NA> NULL KK On Tue, Jan 13, 2009 at 12:49 PM, Gundala Viswanath <gundalav@gmail.com>wrote:> Dear all, > > Suppose I have a hash created with this > > x <- list() > for (i in c('test', 'some', 'more')){ > x[[i]] <- runif(1) > } > > then I want to extract the elem of that hash with > a vector > > > q <- c("some", "more", "not_there") > > But why this failed? > > > extracted <- x[[q]] > Error in x[[q]] : subscript out of bounds > > we expect the output of 'extracted' to be > a vector as well. When the key is not present > to give "NA" in vector > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >[[alternative HTML version deleted]]