Re: [R] subsetting lists....

2018-06-18 Thread MacQueen, Don via R-help
The unlist solution is quite clever. But I will note that none of the solutions offered so far succeed if the input is, for example, YH <- list(1:5, letters[1:3], 1:7) iuhV <- c(2,2,4) and the desire is to return a list whose elements are of the same types as the input list. Which would

Re: [R] subsetting lists....

2018-06-18 Thread Berry, Charles
> On Jun 18, 2018, at 4:15 AM, akshay kulkarni wrote: > > correctionI want the method without a for loop Here are two. The first is more readable, but the second is 5 times faster. mapply("[", YH, iuhV) unlist(YH, recursive = FALSE, use.names = FALSE)[cumsum( lengths(YH)) - lengths(YH)

Re: [R] subsetting lists....

2018-06-18 Thread Eric Berger
sapply( 1:length(YH), function(i) { YH[[i]][iuhV[i]]}) On Mon, Jun 18, 2018 at 1:55 PM, akshay kulkarni wrote: > dear members, > I have list YH and index vector iuhV. I want > to select iuhV[1] from YH[[1]], iuhv[2] from YH[[2]], iuhv[3] from > YH[[3]]..iuhv[n] f

[R] subsetting lists....

2018-06-18 Thread akshay kulkarni
dear members, I have list YH and index vector iuhV. I want to select iuhV[1] from YH[[1]], iuhv[2] from YH[[2]], iuhv[3] from YH[[3]]..iuhv[n] from YH[[n]]... How to do this? I searched SO and the internet but was bootless Very many thanks for your time and e

Re: [R] Subsetting Lists

2010-07-12 Thread Jorge Ivan Velez
Hi Andrew, Try sapply(lst, "[", 2) HTH, Jorge On Mon, Jul 12, 2010 at 3:12 PM, Andrew Leeser <> wrote: > I am looking for a way to create a vector which contains the second element > of > every vector in the list. However, not every vector has two components, so > I > need to generate an NA f

[R] Subsetting Lists

2010-07-12 Thread Andrew Leeser
I am looking for a way to create a vector which contains the second element of every vector in the list. However, not every vector has two components, so I need to generate an NA for those missing. For example, I have created the following list: lst <- list(c("a", "b"), c("c"), c("d", "e"), c("