I have written a script to read a file from disk and check the values in it and write 3 other files on disk. Unfortunately, something that seemed to be very straight forward came out being a headache. The code is:
Arqcodnegs ="result/lista_de_codnegs.txt"
dirout = "./result/"
Codnegs_fornecidos = c("ABC", "A1B2", "PETR3")
Verifica_codneg = function (Codnegs_fornecidos, Arqcodnegs) {
if (!file.exists(Arqcodnegs)) {
stop("Falta arquivo lista_de_codnegs.txt")
}
Codnegs_lidos = read.table(Arqcodnegs,header=FALSE, sep='\t', quote='\"', stringsAsFactors=TRUE)
Codnegs_negativos = c(setdiff (Codnegs_fornecidos, Arqcodnegs))
Codnegs_positivos = c(intersect (Codnegs_fornecidos, Arqcodnegs))
write.table(Codnegs_lidos, paste(dirout, "lista_de_codnegs_lidos.txt", sep=''), col.names=FALSE, row.names=FALSE, sep='\t')
write.table(Codnegs_negativos, paste(dirout, "lista_de_codnegs_negativos.txt", sep=''), col.names=FALSE, row.names=FALSE, sep='\t')
write.table(Codnegs_positivos, paste(dirout, "lista_de_codnegs_positivos.txt", sep=''), col.names=FALSE, row.names=FALSE, sep='\t')
}
The file "lista_de_codnegs.txt" has the following values in it:
"PDGR3" "PETR3" "PETR4"
As expected, the file "lista_de_codnegs_lidos.txt" returns the appropriate values in one single column, meaning "PDGR3", "PETR3" and "PETR4".
But, the main problems are:
The file "lista_de_codnegs_negativos.txt" returns "ABC", "A1B2", "PETR3", but the it should have returned "ABC" and "A1B2", only.
The file "lista_de_codnegs_positivos.txt" returns no values, but it should have returned "PETR3".
What am I doing wrong?