Calculate name number

I would like to calculate the name number for a set of given names.

The name number is calculated by summing the value assigned to each alphabet. The values ​​are shown below:

a=i=j=q=y=1
b=k=r=2
c=g=l=s=3
d=m=t=4
h=e=n=x=5
u=v=w=6
o=z=7
p=f=8

      

Example: David's name number can be calculated as follows:

D+a+v+i+d
4+1+6+1+4
16=1+6=7

      

David's name number is 7.

I would like to write a function in R for this. I am grateful for any guidance or advice or package suggestions I should look into.

+3


source to share


3 answers


I'm sure there are several ways to do this, but here's a named vector approach:



x <- c(
  "a"=1,"i"=1,"j"=1,"q"=1,"y"=1,
  "b"=2,"k"=2,"r"=2,
  "c"=3,"g"=3,"l"=3,"s"=3,
  "d"=4,"m"=4,"t"=4,
  "h"=5,"e"=5,"n"=5,"x"=5,
  "u"=6,"v"=6,"w"=6,
  "o"=7,"z"=7,
  "p"=8,"f"=8)
##
name_val <- function(Name, mapping=x){
  split <- tolower(unlist(strsplit(Name,"")))
  total <-sum(mapping[split])
  ##
  sum(as.numeric(unlist(strsplit(as.character(total),split=""))))
}
##
Names <- c("David","Betty","joe")
##
R> name_val("David")
[1] 7
R> sapply(Names,name_val)
David Betty   joe 
    7     7     4 

      

+5


source


This piece of code will accomplish what you want:

# Name for which the number should be computed.
name <- "David"

# Prepare letter scores array. In this case, the score for each letter will be the array position of the string it occurs in.
val <- c("aijqy", "bkr", "cgls", "dmt", "henx", "uvw", "oz", "pf")

# Convert name to lowercase.
lName <- tolower(name)             

# Compute the sum of letter scores.
s <- sum(sapply(unlist(strsplit(lName,"")), function(x) grep(x, val)))

# Compute the "number" for the sum of letter scores. This is a recursive operation, which can be shortened to taking the mod by 9, with a small correction in case the sum is 9.
n <- (s %% 9)
n <- ifelse(n==0, 9, n)

      



'n' is the result you want for any "name"

+10


source


You need to create a vector of values ​​in alphabetical order and then use match

to get their indices. Something like that:

a <- i <- j <- q <- y <- 1
b <- k <- r <- 2
c <- g <- l <- s <- 3
d <- m <- t <- 4
h <- e <- n <- x <- 5
u <- v <- w <- 6
o <- z <- 7
p <- f <- 8

vals <- c(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
sum(vals[match(c("d","a","v","i","d"), letters)])

      

+6


source







All Articles