[R] Extracting the first element of a list
Philippe Grosjean
phgrosje at ulb.ac.be
Wed Mar 27 14:23:57 CET 2002
as.numeric(substr(as.character(foo),1,1))
does what you are looking for, although I don't understand why you should be
interested by collecting digit "3" from 305, together with digit "1" from
17. If you simply want to extract hundreds from numbers (thus, you will get
"0" for 17), then you could use:
floor(foo/100) - floor(foo/1000)*10
Best,
Philippe Grosjean
...........]<(({°<...............<°}))><...............................
) ) ) ) ) __ __
( ( ( ( ( |__) | _
) ) ) ) ) | hilippe |__)rosjean
( ( ( ( ( Marine Biol. Lab., ULB, Belgium
) ) ) ) ) __
( ( ( ( ( |\ /| |__)
) ) ) ) ) | \/ |ariculture & |__)iostatistics
( ( ( ( (
) ) ) ) ) e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( SciViews project coordinator (http://www.sciviews.org)
) ) ) ) ) tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
) ) ) ) ) "I'm 100% confident that p is between 0 and 1"
( ( ( ( ( L. Gonick & W. Smith (1993)
) ) ) ) )
.......................................................................
-----Message d'origine-----
De : owner-r-help at stat.math.ethz.ch
[mailto:owner-r-help at stat.math.ethz.ch]De la part de Ko-Kang Kevin Wang
Envoyé : mercredi 27 mars 2002 11:48
À : Uwe Ligges; Ross Darnell
Cc : r-help at stat.math.ethz.ch
Objet : Re: [R] Extracting the first element of a list
Hi,
----- Original Message -----
From: "Uwe Ligges" <ligges at amadeus.statistik.uni-dortmund.de>
To: "Ross Darnell" <r.darnell at shrs.uq.edu.au>
Cc: <r-help at stat.math.ethz.ch>
Sent: Wednesday, March 27, 2002 8:03 PM
Subject: Re: [R] Extracting the first element of a list
> 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])
So, suppose I've got a vector called foo, which has elements as follows:
305 159 251 215 101 224 306 199 194 325 329 221 318 238 17 ....
(the rest omittied)
And suppose I want to extract out the first digit of each number, so like:
3 1 2 2 1 2 3 1 1 3 3 2 3 2 1...
Do I still use
lapply(foo, function(x) x[1])
it doesn't seem to work...
Thanks
Kevin
>
> 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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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