Hello everyone!
I want to bind a tool that gives me an overall score for a certain character. Meaning that my input is a character and for every position a certain letter gets assigned with a certain score. In the end the function adds all values and gives you an overall score for the character input. That is what I did:
MaN <- matrix(
data = c(0,0,0,0,0,0,2,0,2,
0,3,0,3,0,0,0,3,0,
0,0,0,0,3,3,0,0,0,
2,0,2,0,0,0,0,0,0),
ncol = 9, byrow = TRUE, dimnames = list(LETTERS[c(1,2,3,4)],paste0('pos',1:9))
)
Letter_func <- function(vec, MaN){
mat <- strsplit(vec, split = "")[[1]] |> as.matrix() |> `rownames<-`(paste0('pos',1:9))
res <- vector(length = 1)
for (i in seq.default(1,ncol(MaN))){
res <- res + MaN[[which(mat[[i]] == rownames(MaN)),i]]
}
return(res)
}
That would give me for example:
Letter_func(c("DBDBCCABA"), MaN)
>23
Letter_func(c("DADACCABA"), MaN)
>17
That works so far. However, I would like to take this to the next level and assign a score to each position that depends on the neighbouring letters. I hope one can understand what I am saying. Just an example. If the first 3 position are ABA I would get a score of 3 for this 3 position (because of the second position. However, I want to tell the function that it gives me a different score when the neighbouring letters got a 0. So in this ABA case I would want the function to give me a score of 1 cause next to pos 2 there are only letters with scores of 0. However, if the character starts with ABD, i would want a score of 5 cause now the B has a neighbour that has a score of 2.
My idea was to divide the character into triplets that include the neighbours . So that character DADACCABA would be divided into DA, DAD, ADA, DAC, ACC, CCA, CAB,ABA, BA. Then I could for every position assign a score for each possible combination of these 4 letters. However, I dont know how to do that and I am very lost.
Can someone help me? or maybe also someone has a better idea how to take neighbours into account?
I would be super thankful for every help and really want to learn!