Help with Speed Up R code

Hi, I have a code to generate a csv table, but it takes too long to be completed, is there a way to speed up it? maybe vectorise it? R code follows below.


{
Tester <- read.csv("Tester.csv", stringsAsFactors = F, h = F)
Female <- read.csv("Female.csv", stringsAsFactors = F, h = F)
#Markers <- read.csv("Markers.csv", stringsAsFactors = F, h = F)
#}
#{
Tester <- Tester[,-c(2,3)]
#}
#{
  Tester[Tester == "AA"] <- "A"
  Tester[Tester == "CC"] <- "C"
  Tester[Tester == "TT"] <- "T"
  Tester[Tester == "GG"] <- "G"
  Tester[Tester == "AT"] <- "N"
  Tester[Tester == "TA"] <- "N"
  Tester[Tester == "AC"] <- "N"
  Tester[Tester == "CA"] <- "N"
  Tester[Tester == "AG"] <- "N"
  Tester[Tester == "GA"] <- "N"
  Tester[Tester == "GC"] <- "N"
  Tester[Tester == "CG"] <- "N"
  Tester[Tester == "GT"] <- "N"
  Tester[Tester == "TG"] <- "N"
  Tester[Tester == "CT"] <- "N"
  Tester[Tester == "TC"] <- "N"
  Tester[Tester == "./."] <- "N"
#}
#{
Female <- Female[,-c(2,3)]
#}
#{
Female[Female == "AA"] <- "A"
Female[Female == "CC"] <- "C"
Female[Female == "TT"] <- "T"
Female[Female == "GG"] <- "G"
Female[Female == "AT"] <- "N"
Female[Female == "TA"] <- "N"
Female[Female == "AC"] <- "N"
Female[Female == "CA"] <- "N"
Female[Female == "AG"] <- "N"
Female[Female == "GA"] <- "N"
Female[Female == "GC"] <- "N"
Female[Female == "CG"] <- "N"
Female[Female == "GT"] <- "N"
Female[Female == "TG"] <- "N"
Female[Female == "CT"] <- "N"
Female[Female == "TC"] <- "N"
Female[Female == "./."] <- "N"
#}
#{
x = ncol(Female)
y = ncol(Tester)
z = ((ncol(Female)-1)*(ncol(Tester)-1))+1
w = nrow(Female)
j = 2
k = 2

Hybrids <- as.data.frame(matrix(nrow=w), ncol=z)
Hybrids[ ,1] <- paste (Tester[ ,1])
#}
#{
while(j<y+1) {
  for (i in 2:x){
    Hybrids[ , k] <- paste (Female[ , i], Tester[ , j], sep="/")
    i = i + 1
    k = k + 1     
  }
  j = j + 1      
}
}
{
#Hybrids <- as.data.frame(t(Hybrids))
write.table(Hybrids, "Hybrid_Genotypes.csv", sep=",")
}

A first step might be to run the Profiler to see which lines take the most time.

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.