Export matrix to csv in R

0

I have 3 csv with 3050 lines each, what I want to do is put the 3 together and add additional data and then take it out in a new csv. But when exporting the resulting matrix I find that the format is not correct.

The code is this:

nombres <- read.csv("C:/Users/CASA/Desktop/nombres.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos1 <- read.csv("C:/Users/CASA/Desktop/apellidos1.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos2 <- read.csv("C:/Users/CASA/Desktop/apellidos2.csv", header=TRUE, sep=",", na.strings="NA",dec=",")


total <-3050
clientes<-data.frame(matrix(, nrow=total , ncol=0))

for (ind in 0:total-1){

    clientes$Apellido1<-apellidos1[ind]
      clientes$Apellido2<-apellidos2[ind]
    clientes$Nombre<-nombres[ind]
    clientes$DNI<-generadni()
    clientes$Telf<-sample(600000000:699999999,1)
}
print(clientes)
print("FIN")

#DNI

generadni<-function(){
  valor<-sample(10000000:99999999,1)
  dni<-valor
  valor<-valor/23
  valor<-as.integer(valor)
  valor<-valor*23
  valor<-dni-valor
  #letras<-"TRWAGMYFPDXBNJZSQVHLCKEO"
  letras<-c("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E","O")
  #dni<-as.character(dni)
  #letra<-substr(letras,valor,1)
  letra<-letras[valor]
  resultado<-paste(dni,letra,sep="")
  return(resultado)
}

write.csv2(clientes, "C:/Users/CASA/Desktop/resultado.csv", sep=",")

If I run the script in the console it looks good as in the example:

     APELLIDO1       DNI      Telf
1      Aguilar 17139198B 636536106
2       Alonso 17139198B 636536106
3      Álvarez 17139198B 636536106
...

But when I open the CSV I see it wrong, as if the first column instead of being a text string were c1 (numbers from 1 to 108), DNI, telephone ...

An example:

"";"Apellido1";"DNI";"Telf"
"1";c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,...(to 108)),
    
asked by Marc Vinyes Perez 25.10.2017 в 16:02
source

1 answer

0

Analyzing the first three lines of the code:

nombres <- read.csv("C:/Users/CASA/Desktop/nombres.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos1 <- read.csv("C:/Users/CASA/Desktop/apellidos1.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos2 <- read.csv("C:/Users/CASA/Desktop/apellidos2.csv", header=TRUE, sep=",", na.strings="NA",dec=",")

I understand that these should be data frames and, by the code that follows, they seem to contain only one column with 3050 elements.

total <-3050
clientes<-data.frame(matrix(, nrow=total , ncol=0))

for (ind in 0:total-1){

    clientes$Apellido1<-apellidos1[ind]
    clientes$Apellido2<-apellidos2[ind]
    clientes$Nombre<-nombres[ind]
    clientes$DNI<-generadni()
    clientes$Telf<-sample(600000000:699999999,1)
}

This loop does not make sense.

  • The ind index is used to access surnames and names, but not clientes$Apellido1 (nor the others)
  • All the columns defined in this way end up being equal to what the last index is worth.

I think the code should be:

nombres <- read.csv("C:/Users/CASA/Desktop/nombres.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos1 <- read.csv("C:/Users/CASA/Desktop/apellidos1.csv", header=TRUE, sep=",", na.strings="NA",dec=",")
apellidos2 <- read.csv("C:/Users/CASA/Desktop/apellidos2.csv", header=TRUE, sep=",", na.strings="NA",dec=",")

generadni<-function(){
  valor<-sample(10000000:99999999,1)
  dni<-valor
  valor<-valor/23
  valor<-as.integer(valor)
  valor<-valor*23
  valor<-dni-valor
  #letras<-"TRWAGMYFPDXBNJZSQVHLCKEO"
  letras<-c("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E","O")
  #dni<-as.character(dni)
  #letra<-substr(letras,valor,1)
  letra<-letras[valor]
  resultado<-paste(dni,letra,sep="")
  return(resultado)
}

clientes <- data.frame(Nombre = nombres, 
                       Apellido1 = apellidos1, 
                       Apellido2 = apellidos2, 
                       DNI = replicate(length(apellidos1), generadni()),
                       Telf = sample(600000000:699999999, length(apellidos1), replace = T),
                       stringsAsFactors = F)

write.csv2(x = clientes, 
           file = "C:/Users/CASA/Desktop/resultado.csv",
           row.names = F,
           sep = ",",
           quote = F)

It seems to me that the error in the recorded csv is because it saves the line numbers. This is avoided with the option row.names = F .

    
answered by 04.11.2017 в 05:21