Re: [R] Calculating sum of letter values

2008-11-24 Thread William Dunlap
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,""

Re: [R] Calculating sum of letter values

2008-11-24 Thread Berwin A Turlach
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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Rory.WINSTON
.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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Stefan Evert
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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Marc Schwartz
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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Richard . Cotton
> 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 > >

Re: [R] Calculating sum of letter values

2008-11-24 Thread Jagat.K.Sheth
, 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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Gabor Grothendieck
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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Rory.WINSTON
(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

Re: [R] Calculating sum of letter values

2008-11-24 Thread Marc Schwartz
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

[R] Calculating sum of letter values

2008-11-24 Thread Rory.WINSTON
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