Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple elements from multiple vectors in a list

Is there a way to select multiple elements of multiple vectors stored in a list? The position of each element I am interested in selecting for each vector is the same. Consider the following MWE of a list with a length of 3. Each vector within the list is of equal length too.

# create list and vectors within storing random data
mylist<- list(rnorm(5), rgeom(5, 0.05), rbinom(5, 10, 0.5))

If I want to select the 1st and 3rd element of the 1st vector I write mylist[[1]][c(1,3)] and this returns the values of elements in position 1 and 3. Similarly, for the second vector, I write mylist[[2]][c(1,3)].

But how can I write some neat code so that I can select the 1st and 3rd element of both the 1st and 2nd vectors in the list at the same time? I have tried mylist[c(1, 2)][c(1, 3)] but this returns all the elements of the 1st vector and NULL for the second vector.

EDIT

Once the elements have been selected how does one overwrite them, such that (for example) mylist[c(1, 2)][c(1,3)] <- 0

like image 583
altfi_SU Avatar asked Nov 27 '19 14:11

altfi_SU


People also ask

How do you select multiple elements in a list?

To select multiple items in a list, hold down the Ctrl (PC) or Command (Mac) key. Then click on your desired items to select. All of the items you have selected should be highlighted with a different-colored background. Note: Be sure to hold the Ctrl (PC) or Command (Mac) key down while selecting multiple items.

How do you select all elements in a list?

To select elements from a Python list, we will use list. append(). We will create a list of indices to be accessed and the loop is used to iterate through this index list to access the specified element. And then we add these elements to the new list using an index.

How do you find the common element of multiple vectors?

1 Answer. To find the common elements from multiple vectors, you can use the intersect function from the sets base R package. vectors (of the same mode) containing a sequence of items (conceptually) with no duplicate values. Intersect will discard any duplicated values in the arguments, and they apply as.

How do you find common elements in multiple lists in R?

First of all, create a number of vectors. Use intersect function to find the common elements in all the vectors.


2 Answers

We could use lapply

lapply(mylist[1:2], `[`, c(1, 3))

#[[1]]
#[1] -0.5604756  1.5587083

#[[2]]
#[1]  6 55

which is similar to map in purrr

purrr::map(mylist[1:2], `[`, c(1, 3))

To update the values of selected elements, we can do

mylist[1:2] <-lapply(mylist[1:2], function(x) {x[c(1, 3)] <- 0;x})
mylist
#[[1]]
#[1]  0.00000000 -0.23017749  0.00000000  0.07050839  0.12928774

#[[2]]
#[1]  0 61  0  8  8

#[[3]]
#[1] 4 3 8 7 6

data

set.seed(123)
mylist<- list(rnorm(5), rgeom(5, 0.05), rbinom(5, 10, 0.5))
like image 98
Ronak Shah Avatar answered Oct 21 '22 11:10

Ronak Shah


We can use map_at

library(purrr)
map_at(mylist, 1:2, ~ replace(.x, c(1, 3), 0))
#[[1]]
#[1]  0.00000000 -0.23017749  0.00000000  0.07050839  0.12928774

#[[2]]
#[1]  0 61  0  8  8

#[[3]]
#[1] 4 3 8 7 6

data

set.seed(123)
mylist<- list(rnorm(5), rgeom(5, 0.05), rbinom(5, 10, 0.5)
like image 28
akrun Avatar answered Oct 21 '22 10:10

akrun