[R] Calculating sum of letter values

Rory.WINSTON at rbs.com Rory.WINSTON at rbs.com
Mon Nov 24 16:14:53 CET 2008


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, so that instead of:

as.numeric(factor(unlist(strsplit("XYZ",""))))
[1] 1 2 3

I would expect to see

[1] 24 25 26

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,""), 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:marc_schwartz at comcast.net]
Sent: 24 November 2008 15:09
To: WINSTON, Rory, GBM
Cc: r-help at r-project.org
Subject: Re: [R] Calculating sum of letter values

on 11/24/2008 08:57 AM Rory.WINSTON at rbs.com 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(LETTERS %in% unlist(strsplit("ABCDA", ""))) is not  quite
> correct, as it does not count repeated characters. I guess what I need is some kind of lookup table?
>
> Cheers
> Rory


> sum(as.numeric(factor(unlist(strsplit("ABCDA", "")))))
[1] 11


Convert the letters to factors, after splitting the vector, which then enables the use of the underlying numeric codes:

> as.numeric(factor(unlist(strsplit("ABCDA", ""))))
[1] 1 2 3 4 1

HTH,

Marc Schwartz

***********************************************************************************
The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB. 
Authorised and regulated by the Financial Services Authority 

This e-mail message is confidential and for use by the=2...{{dropped:22}}



More information about the R-help mailing list