I have here a list of genetic loci containing their alleles encoded as three digit numbers, as class character. I have a few lines of code to go through the list and convert all instances to nucleic base letters (ie. A, C, G, T).
my_allele_list = list(loc1 = c("001", "002"),
loc2 = c("001", "003"),
loc3 = c("004", "001"),
loc4 = c("003", "003"),
loc5 = c("001", "002"),
loc6 = c("002", "004"))
a = c("001", "002", "003", "004")
b = c("A", "C", "G", "T")
for(i in seq_along(a)) my_allele_list <-
lapply(my_allele_list, function(x) gsub(a[i], b[i], x))
my_allele_list
So far so good, but to keep things tidy I would like to wrap these lines into a function.
convert_alleles <- function(x){
a = c("001", "002", "003", "004")
b = c("A", "C", "G", "T")
for(i in seq_along(a)) x <-
lapply(x, function(x) gsub(a[i], b[i], x))
}
convert_alleles(my_allele_list)
my_allele_list
However, as you can see on this second pass the function does not work - no error, just no change made to the list object. I suspect that the issue is with a clash with the anonymous function within the for loop. Can someone explain what the issue is and suggest a solution?