Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How do I remove the first element from each inner element of a list without converting it to matrix?

Tags:

r

nested-lists

I have a list like that

[[1]]
[1] a1 b1 c1
[[2]]
[1] a2 b2 c2
[[3]]
[1] a3 b3 c3

I want specific element removed from each part of it:

[[1]]
[1] a1 c1
[[2]]
[1] a2 c2
[[3]]
[1] a3 c3

I tried tail but removes "outer" elements. Maybe some indexing would do?

like image 431
ephemeris Avatar asked Jan 18 '16 15:01

ephemeris


People also ask

How do I drop the first element in a list?

The pop() method is one of the list object method used to remove an element from the list and returns it. While using the pop() method, you have to specify the index value of the element in the list as an argument and return the popped out element as the desired output.

How do I remove the first element from a vector in R?

To delete an item at specific index from R Vector, pass the negated index as a vector in square brackets after the vector.

How do I remove a specific value from a vector in R?

Declare a boolean vector that has TRUE at all the positions you want to retain and FALSE at those you want to delete. Suppose that vector is y. Then, x[y] will give you the requires output.


1 Answers

Assuming the pattern is just that you want the second element removed,

lapply(List, function(x) x[-2])
like image 110
Señor O Avatar answered Oct 17 '22 01:10

Señor O