[R] Calculating sum of letter values
Marc Schwartz
marc_schwartz at comcast.net
Mon Nov 24 16:23:59 CET 2008
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
>
More information about the R-help
mailing list