[R] Calculating sum of letter values

William Dunlap wdunlap at tibco.com
Mon Nov 24 20:06:38 CET 2008


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,""),
function(x) match(x,LETTERS) )) ) } )
> 
> But this is kind of ugly....
> 
> Rory Winston
> RBS Global Banking & Markets
> Office: +44 20 7085 4476

Do you mean that the nested lapply's are kind of ugly.  You don't
need them.  I think the following does the same as what you wrote

 f1 <- function(namelist)lapply(strsplit(namelist,""), function(x)
sum(match(x,LETTERS)))

where your code as a function would be
 
 f0 <- function(namelist)lapply(namelist, function(Z) { sum (
unlist(lapply(strsplit(Z,""), function(x) match(x,LETTERS) )) ) } )

(Since f0() and f1() return lists of scalar integers, it might make more
sense to call unlist() on their outputs before returning them.)

Another approach is to use a named vector of character values to map
characters to values, such as in

 f2 <- function(namelist) {
     values <- c(seq_along(LETTERS), seq_along(letters), 0L, 0L, 0L)
     names(values) <- c(LETTERS, letters, " ", "-", ".")
     lapply(strsplit(namelist,""), function(characters,
values)sum(values[characters]), values)
 }

E.g.,
  > f2(c("Mary Jean", "Maryjean", "Mary-Jean", "MARYJEAN"))
  [[1]]
  [1] 87

  [[2]]
  [1] 87

  [[3]]
  [1] 87

  [[4]]
  [1] 87

That approach lets you map several characters to the same value, and the
values are not restricted to the small positive integers
1:length(possibleCharacters).
  
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 



More information about the R-help mailing list