I have a named list of strings:
str(nr.genes)
Named chr [1:37] "0" "0" "Pipas_c034_0013" "Pipas_chr1-4_0040" ...
- attr(*, "names")= chr [1:37] "Nr11" "Nr12" "Nr131" "Nr132" ...
I want to substitute the string names for those in the next object, but not 1-to-1 :
str(nr.genes.names)
chr [1:16] "up.p33-dw.p33" "up.p33-dw.p38" "up.p33-dw.p52" ...
I would like to have something like this:
str(nr.genes)
Named chr [1:37] "0" "0" "Pipas_c034_0013" "Pipas_chr1-4_0040" ...
- attr(*, "names")= chr [1:37] "up.p33-dw.p33" "up.p33-dw.p38" "up.p33-dw.p52" "up.p33-dw.p52" ...
Where I have changed manually
"Nr11"<-"up.p33-dw.p33"
"Nr12"<-"up.p33-dw.p38"
"Nr131"<-"up.p33-dw.p52"
"Nr132"<-"up.p33-dw.p52" #Note that the third digit doesn't matter
.
.
.
I need to replace each string's name depending on the first 2 digits, with the elements of the second list. I have tried with an lapply function, but I didn't manage, I also tried creating a function involving sub
and replacement, but I couldn't see any change, here is the code below:
lfun <- function(x,y) {
for(o in 1:16){ #Length of the names to substitue for
for (p in 1:4){
for (q in 1:4){
x[sub(paste("Nr", p, q, sep=""), x[o], replacement=y[o])]
}
}
}
}
With this function I try to get all the names of the attributes of the first argument and substitute them by the element it implies. But it returns the same I had previously without any warning or error message. I'm sure there must be a easy way to do it, but I don't find the correct
Edit: nr.genes can be created as following
nr.genes <- c("0", "0", "Pipas_c034_0013", "Pipas_chr1-4_0040")
attr(nr.genes, "names") <-c("Nr11", "Nr12", "Nr131", "Nr132")
and nr.genes. names by
nr.genes.names <- c("up.p33-dw.p33", "up.p33-dw.p38", "up.p33-dw.p52")
About the matching I need that Nr11 <-"up.p33-dw.p33"
, but Nr21<-"<nr.genes.names[5]>"
, then Nr22<-"<nr.genes.names[6]>",
then Nr23<-"<nr.genes.names[7]>"
,then Nr24<-"",then Nr31 <-"<nr.genes.names[9]>"