Rory Winston wrote:
> I have got it to work in a fairly non-elegant manner, using the
following code:
>
> sum ( unlist(lapply(strsplit("TESTING",""), function(x)
match(x,LETTERS) )) )
>
> And over a list of names, this becomes:
>
> lapply(namelist, function(Z) { sum ( unlist(lapply(strsplit(Z,""
G'day Rory,
On Mon, 24 Nov 2008 14:57:57 +
<[EMAIL PROTECTED]> wrote:
> If I have a string, say "ABCDA", and I want to convert this to the
> sum of the letter values, e.g.
>
> A -> 1
> B -> 2
>
> etc, so "ABCDA" = 1+2+3+4+1 = 11
>
> Is there an elegant way to do this? [...]
R> sum(as.nume
.org
Subject: Re: [R] Calculating sum of letter values
Yep, my error...it should be:
> as.numeric(factor(unlist(strsplit("ABCDA", "")), levels = LETTERS))
[1] 1 2 3 4 1
> as.numeric(factor(unlist(strsplit("XYZ", "")), levels = LETTERS))
[1] 24 25 26
The s
Thanks, that's almost exactly what I need...theres just a slight
difference with my requirement, in that I am looking for the actual
index value in the alphabetical sequence, so that instead of:
as.numeric(factor(unlist(strsplit("XYZ",""
[1] 1 2 3
I would expect to see
[1] 24 25 26
ly
>
> Rory Winston
> RBS Global Banking & Markets
> Office: +44 20 7085 4476
>
> -Original Message-
> From: Marc Schwartz [mailto:[EMAIL PROTECTED]
> Sent: 24 November 2008 15:09
> To: WINSTON, Rory, GBM
> Cc: r-help@r-project.org
> Subject: Re: [R] C
> Thanks, that's almost exactly what I need...theres just a slight
> difference with my requirement, in that I am looking for the actual
> index value in the alphabetical sequence, so that instead of:
>
> as.numeric(factor(unlist(strsplit("XYZ",""
> [1] 1 2 3
>
> I would expect to see
>
>
, 2008 9:15 AM
To: [EMAIL PROTECTED]
Cc: r-help@r-project.org
Subject: Re: [R] Calculating sum of letter values
Hi Mark
Thanks, that's almost exactly what I need...theres just a slight
difference with my requirement, in that I am looking for the actual
index value in the alphabetical sequence, s
Here are a couple of solutions.
The first matches each
character against LETTERS returning the position number
in LETTERS of the match. strsplit returns a list of which
we want the first element and then we sum that.
The second applies function(x) match(x, LETTERS),
which is specified in formula
(strsplit(Z,""), function(x)
match(x,LETTERS) )) ) } )
But this is kind of ugly
Rory Winston
RBS Global Banking & Markets
Office: +44 20 7085 4476
-Original Message-
From: Marc Schwartz [mailto:[EMAIL PROTECTED]
Sent: 24 November 2008 15:09
To: WINSTON, Rory, GBM
C
on 11/24/2008 08:57 AM [EMAIL PROTECTED] wrote:
> Hi all
>
> If I have a string, say "ABCDA", and I want to convert this to the sum of the
> letter values, e.g.
>
> A -> 1
> B -> 2
>
> etc, so "ABCDA" = 1+2+3+4+1 = 11
>
> Is there an elegant way to do this? Trying something like
>
> which(LET
Hi all
If I have a string, say "ABCDA", and I want to convert this to the sum of the
letter values, e.g.
A -> 1
B -> 2
etc, so "ABCDA" = 1+2+3+4+1 = 11
Is there an elegant way to do this? Trying something like
which(LETTERS %in% unlist(strsplit("ABCDA", "")))
is not quite correct, as it does
11 matches
Mail list logo