ALs
ALs

Reputation: 509

removing values from each element in a list in R

I was wondering if somebody could help with this problem. I have a list coming out from a function similar to the following:

lis<-vector("list",3)
lis[[1]]<-c(1,2,3)
lis[[2]]<-c(1,2,3)
lis[[3]]<-c(1,2,3)

so it looks like

[[1]]
[1] 1 2 3
[[2]]
[1] 1 2 3
[[3]]
[1] 1 2 3

What I want to do is remove, for example, the first element from each component of the list so it ends up like:

[[1]]
[1] 2 3
[[2]]
[1] 2 3
[[3]]
[1] 2 3

Any ideas would be most welcome.

Upvotes: 0

Views: 67

Answers (1)

jogo
jogo

Reputation: 12559

You can use lapply() and do the index function for each element of the list. The index -1 means without the first element:

lis <- list(a=1:3, b=11:13, c=21:23)
lapply(lis, '[', -1)
# $a
# [1] 2 3
# 
# $b
# [1] 12 13
# 
# $c
# [1] 22 23

Upvotes: 2

Related Questions