I have a vector mynumbers
with several strings of numbers, say:
mynumbers <- c("122212", "134134", "134134", "142123", "212141", "213243", "213422", "214231", "221233")
My goal is to translate such strings into strings of letters following these relationships:
1=A
2=C
3=G
4=T
I'd like to encapsulate this in a function so that:
myletters <- translate_function(mynumbers)
myletters
would thus be:
myletters <- c("ACCCAC", "AGTAGT", "AGTAGT", "ATCACG", "CACATA", "CAGCTG", "CAGTCC", "CATCGA", "CCACGG")
I'm thinking of a function like this, obviously not correct... I start to get confused when dealing with strsplit
and lists...
translate_function <- function(numbers){
map_df <- data.frame(num=1:4, nuc=c('A','C','G','T'))
#strsplit numbers
split_numbers <- strsplit(numbers, '')
letters <- paste(sapply(split_numbers, function(x) map_df$nuc[which(map_df$num==x)]), collapse='')
return(letters)
}
What would be the easiest and most elegant way to accomplish this? Thanks!