Your data has a very strange format because it is an array with text and numbers. Probably the ideal would be to replace the NAO INFORMADO
with NA
, when reading the values. In the current way, you can do the following:
dados <- read.table(text="NAO INFORMADO, 1
2, 3
NAO INFORMADO, 5
4, 1
NAO INFORMADO, 3
6, 5", sep = ",", stringsAsFactors = FALSE)
dados[dados[,1] == "NAO INFORMADO",1] <- dados[dados[,1] == "NAO INFORMADO",2]
dados
# V1 V2
#1 1 1
#2 2 3
#3 5 5
#4 4 1
#5 3 3
#6 6 5
But if they were NA
, it would be a little simpler:
dados[is.na(dados[,1]),1] <- dados[is.na(dados[,1]),2]
Note that this form considers that your data is an array. It would also work if it were a data.frame
, but in this case it would be possible to write a slightly more elegant code.