[R] Extracting the first element of a list
Mike Lonergan
mel at mcs.st-and.ac.uk
Wed Mar 27 15:00:57 CET 2002
How about:
> initial.digit<-function(x) {floor(x/10^floor(log(x,10)))}
> lapply(l,initial.digit)
Unless you like strings, in which case:
> initial.digit<-function(x) {substr(as.character(x),1,1)}
> lapply(l,initial.digit)
Seems to do much the same thing.
Though there probably is an easier way of avoiding rounding up. (?)
It was uncooperative because you were asking for the first element of a
number. Since a number is treated as a single 'thing', however many digits
it has, asking for a number's first element gets the whole lot. So to get a
single digit it is necessary to 'explain to the machine how you want to
break things up'.
I hope this makes some sense.
Cheers,
Mike.
> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Ko-Kang
> Kevin Wang
> Sent: 27 March 2002 10:48
> To: Uwe Ligges; Ross Darnell
> Cc: r-help at stat.math.ethz.ch
> Subject: 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