Re: [R] Calculating sum of letter values

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Mon, 24 Nov 2008 09:23:59 -0600

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 step that I missed was setting the factor levels to the full set of LETTERS. HTH, Marc

on 11/24/2008 09:14 AM Rory.WINSTON_at_rbs.com wrote:
> 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
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 24 Nov 2008 - 15:26:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 24 Nov 2008 - 16:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive