Skip to content
Prev 18279 / 398503 Next

Extracting the first element of a list

Dear Kevin,
At 10:48 PM 3/27/2002 +1200, Ko-Kang Kevin Wang wrote:

            
The lapply function takes a list as its first argument, and a vector of 
numbers isn't a list; moreover, each number isn't a vector a individual 
digits that can be extracted by subscripting.

You could use something like the following:

         > x <- c(1, 23, 456, 7890)
         > x %/% 10^floor(log(x, 10))
         [1] 1 2 4 7

If there are negative entries in x, use abs(x).

I hope that this helps,
  John


-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._