Grokbase Groups R r-help March 2002
FAQ
What criteria does one use to decide whether to write a common sequence of
code as a source(procedure) or as a function? As I understand it, both are
maintained as source code and interpreted, so is there any performance
advantage either way?

Thank you for your help.

graham lawrence

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

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

Search Discussions

  • Ross Darnell at Mar 27, 2002 at 12:00 am
    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.


    Thanks
    Ross Darnell

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Peter Dalgaard BSA at Mar 27, 2002 at 12:08 am

    Ross Darnell <r.darnell@shrs.uq.edu.au> writes:

    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. How about
    sapply(strsplit(tmp, " "), "[", 1)
    [1] "A" "B" "BC"


    --
    O__ ---- Peter Dalgaard Blegdamsvej 3
    c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
    (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
    ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Uwe Ligges at Mar 27, 2002 at 8:03 am

    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ko-Kang Kevin Wang at Mar 27, 2002 at 10:48 am
    Hi,

    ----- Original Message -----
    From: "Uwe Ligges" <ligges@amadeus.statistik.uni-dortmund.de>
    To: "Ross Darnell" <r.darnell@shrs.uq.edu.au>
    Cc: <r-help@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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Uwe Ligges at Mar 27, 2002 at 12:12 pm

    Ko-Kang Kevin Wang wrote:
    Hi,

    ----- Original Message -----
    From: "Uwe Ligges" <ligges@amadeus.statistik.uni-dortmund.de>
    To: "Ross Darnell" <r.darnell@shrs.uq.edu.au>
    Cc: <r-help@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...

    Well, I meant *after* strsplit:

    temp <- strsplit(foo, "")
    L <- lapply(temp, function(x) x[1])
    as.numeric(unlist(L))

    In this case you can also calculate it:

    floor(foo / (10^floor(log(foo, 10))))

    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Philippe Grosjean at Mar 27, 2002 at 1:23 pm
    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@amadeus.statistik.uni-dortmund.de>
    To: "Ross Darnell" <r.darnell@shrs.uq.edu.au>
    Cc: <r-help@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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Mike Lonergan at Mar 27, 2002 at 2:00 pm

    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@amadeus.statistik.uni-dortmund.de>
    To: "Ross Darnell" <r.darnell@shrs.uq.edu.au>
    Cc: <r-help@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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • John Fox at Mar 27, 2002 at 2:54 pm
    Dear Kevin,
    At 10:48 PM 3/27/2002 +1200, Ko-Kang Kevin Wang wrote:

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 26, '02 at 8:51p
activeMar 27, '02 at 2:54p
posts9
users8
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase