user8773414
user8773414

Reputation:

Subsetting Lists to extract more than one element

Trying to subset so the subset produces "h" "i"

I've tried l1[[c(2,6)]] which only gives me "h" and l1[[c(2,6:7)]] which gave me an error.

Upvotes: 0

Views: 35

Answers (1)

Joseph Clark McIntyre
Joseph Clark McIntyre

Reputation: 1094

You need to request l1[[2]][c(2, 6)]. l1[[2]] is the second element of l1, and consists of the vector c("c", "d", "e", "f", "g", "h", "i"). You want elements 6 and 7 of that vector, so l1[[2]][c(2, 6)].

l1 <- list(c("a", "b", "c", "d", "e"), c("c", "d", "e", "f", "g", "h", "i"), c("d", "e", "f", "g"))
l1
#[[1]]
#[1] "a" "b" "c" "d" "e"
#
#[[2]]
#[1] "c" "d" "e" "f" "g" "h" "i"
#
#[[3]]
#[1] "d" "e" "f" "g"
#
l1[[2]]
#[1] "c" "d" "e" "f" "g" "h" "i"
l1[[2]][c(6, 7)]
#[1] "h" "i"

Upvotes: 1

Related Questions