Extract same index elements from a list

Ali

This question is an extension of R list get first item of each element.

a <- c(1,2,3)
b <- c(11,22,33)
c <- c(111,222,333)
d <- list(a,b,c)

> sapply(d, function(x) x[1])
  [1]   1  11 111

The code above, extracts the first element of each list. My question is how can I generalize this to obtain a list which extracts all the same index elements and stores them in a list.

My desired output:

[[1]]
[1]   1  11 111

[[2]]
[1]   2  22 222

[[3]]
[1]   3  33 333
ThomasIsCoding

The following may help

sapply(1:3, function(k) sapply(d, function(x) x[k]),simplify = F)

or

Map(function(k) sapply(d, function(x) x[k]), 1:3)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

recursively extract elements from a list of lists

extract elements from a list based on a vector of indices

Extract elements from numpy array, that are not in list of indexes

Extract vectors from a list based on given elements

How to extract different elements of a list at the same time

Extract multiple elements from a list of lists lapply

Extract same elements chunk from list after instance of a particular element

Extract text count from a list of elements

Extract elements from a list of lists - Python Pandas

Pick random integers from list elements of same index and generate a list

Extract the matching number from the list of elements

How to extract the indices of equal elements from a list?

How to extract elements from nested list in R

Web crawler to extract from list elements

Extract subclass elements from List of parent class

Compare two list and get the index of same elements

List from elements with the same key

Starting from two lists, how to put the elements of the same index from each list into a list of tuples

Delete elements of two list with same index in python

Extract elements from different levels of a nested list

How to make a new nested list from an old nested list by merging all the elements at the same index together

extract dictionary elements from nested list in python

How to extract elements from a nested list

Extract Index from a list of dictionaries

Prolog remove elements which have the same value as the index from the list

Remove duplicates from a list and remove elements at same index in another list

How to extract elements from a complex list in Python

Extract elements from list of httr headers

Extract elements from List of List in C#