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
> 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)
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
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
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
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("
6 matches
Mail list logo