[R] Extracting the first element of a list
Uwe Ligges
ligges at amadeus.statistik.uni-dortmund.de
Wed Mar 27 09:03:43 CET 2002
Ross Darnell wrote:
>
> Perhaps some kind person might show me the way to extract the first element of a list.
>
> The example is
>
> tmp <- c("A", "B C","BC D")
>
> I want the first "word" from each of these elements, i.e. "A" ,"B" ,"BC"
>
> strsplit(tmp," ") returns
>
> [[1]]
> [1] "A"
>
> [[2]]
> [1] "B" "C"
>
> [[3]]
> [1] "BC" "D"
>
> I have had no success in trying to extract the first element.
>
> Of course there may be another way to get the same result.
For the first element of a list L:
L[[1]]
For the first elements of all elements (vector) of the list:
lapply(L, function(x) x[1])
Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list