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!
CodePudding user response:
Easily by chartr
,
chartr("1234" , "ACGT", mynumbers)
[1] "ACCCAC" "AGTAGT" "AGTAGT" "ATCACG" "CACATA" "CAGCTG" "CAGTCC" "CATCGA"
[9] "CCACGG"
CodePudding user response:
Use it in a function this way:
translate_function <- function(numbers){
map_df <- data.frame(num=1:4, nuc=c('A','C','G','T'))
letters <- chartr(paste(map_df$num, collapse=''), paste(map_df$nuc, collapse=''), numbers)
return(letters)
}
translate_function(mynumbers)
Output:
[1] "ACCCAC" "AGTAGT" "AGTAGT" "ATCACG" "CACATA" "CAGCTG" "CAGTCC" "CATCGA"
[9] "CCACGG"
But it's better without a dataframe:
translate_function <- function(numbers){
letters <- chartr("1234", "ACGT", numbers)
return(letters)
}
translate_function(mynumbers)
Output:
[1] "ACCCAC" "AGTAGT" "AGTAGT" "ATCACG" "CACATA" "CAGCTG" "CAGTCC" "CATCGA"
[9] "CCACGG"