Hi Andrew, Try
sapply(lst, "[", 2) HTH, Jorge On Mon, Jul 12, 2010 at 3:12 PM, Andrew Leeser <> wrote: > I am looking for a way to create a vector which contains the second element > of > every vector in the list. However, not every vector has two components, so > I > need to generate an NA for those missing. For example, I have created the > following list: > > lst <- list(c("a", "b"), c("c"), c("d", "e"), c("f", "g")) > > > lst > [[1]] > [1] "a" "b" > [[2]] > [1] "c" > [[3]] > [1] "d" "e" > [[4]] > [1] "f" "g" > > I would like the output to be the following: > > > output > [1] "b" NA "e" "g" > > I know I can accomplish this using a for loop, but I am wondering if > there's a > simple and neat way of getting this done in one step? > > Thanks in advance. > > - Andrew > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.