s-news
[Top] [All Lists]

Re: another counting question

To: <mleeds@mlp.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: another counting question
From: <Bill.Venables@csiro.au>
Date: Mon, 30 Aug 2004 15:04:55 +1000
Thread-index: AcSORXh0iqAB3PdGR1q9T1gZKaudGgACM8VQ
Thread-topic: [S] another counting question
Here is a possibility. Let

cv - character vector
nv - numerical vector
xx - new vector to be constructed

Here is a one-liner solution:

        xx <- tapply(nv, cv, sum)[cv]

Warning: untested code.

Bill Venables.




-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Leeds, Mark
Sent: Monday, 30 August 2004 1:57 PM
To: s-news@lists.biostat.wustl.edu
Subject: [S] another counting question


thank you to all the people who wrote
back to me about my counting question.

basically, everyone gave variants of a similar solution
which was to use the function in Splus called table().

now, i have a somewhat more difficult question.
i'm sorry to bother everyone with so
many questions but i think this will
be my last one for a while.

i have one vector that is a list of strings.

say c("AAA","BBB","CCC","BBB","CCC")

and another vector that is a list of numbers
that is the same length as the vector above.

so it might be

c(0.10,0.20,0.34,0.50,0.60)

so, given the string vector above,i would want to return

0.1, 0.7, 0.94, 0.7, 0.94 as the resulting vector.

in other words, whenever the strings are the same,
it should add those numbers up and put them
in the corresponding position. i don't think table can be used here but
maybe there is some other function that does this ?

if making the string vector the names of the columns of
the number vector helps accomplish the task, that
is fine. i just don't want to loop.

                                                                thanks


                                                                    mark

<Prev in Thread] Current Thread [Next in Thread>