Create data.table and export to txt

1

The final goal is to create a txt where each row is different. For this I have different variables with different values that are going to be combined. The solution I provide does not seem the most appropriate because if the number of values is greater the code to write would be very long, perhaps a for , the problem that I see that the first line of the output is different to the following, and that would be repeated for a new value of the variable people.

Code

personas <- data.table(c("Luis","Pedro","Mar"))

año <- "2018"

mes <-"05"

dia <- "23"

dia2 <- data.table(c("lunes","martes","miercoles","jueves","Viernes"))

salida <-data.table(paste(año,mes,dia,personas[1],"&",sep=""),
                    paste(año,mes,dia,personas[1],dia2[1],"&",sep=""),
                    paste(año,mes,dia,personas[1],dia2[2],"&",sep=""),
                    paste(año,mes,dia,personas[1],dia2[3],"&",sep=""),
                    paste(año,mes,dia,personas[1],dia2[4],"&",sep=""),
                    paste(año,mes,dia,personas[1],dia2[5],"&",sep=""),
                    paste(año,mes,dia,personas[2],dia2[1],"&",sep=""),
                    paste(año,mes,dia,personas[2],dia2[2],"&",sep=""),
                    paste(año,mes,dia,personas[2],dia2[3],"&",sep=""),
                    paste(año,mes,dia,personas[2],dia2[4],"&",sep=""),
                    paste(año,mes,dia,personas[2],dia2[5],"&",sep=""),
                    paste(año,mes,dia,personas[3],dia2[1],"&",sep=""),
                    paste(año,mes,dia,personas[3],dia2[2],"&",sep=""),
                    paste(año,mes,dia,personas[3],dia2[3],"&",sep=""),
                    paste(año,mes,dia,personas[3],dia2[4],"&",sep=""),
                    paste(año,mes,dia,personas[3],dia2[5],"&",sep=""))

salida

salida2 <- t(salida)

The output I get is:

    [,1]                     
V1  "20180524Luis&"          
V2  "20180524Luislunes&"     
V3  "20180524Luismartes&"    
V4  "20180524Luismiercoles&" 
V5  "20180524Luisjueves&"    
V6  "20180524LuisViernes&"   
V7  "20180524Pedrolunes&"    
V8  "20180524Pedromartes&"   
V9  "20180524Pedromiercoles&"
V10 "20180524Pedrojueves&"   
V11 "20180524PedroViernes&"  
V12 "20180524Marlunes&"      
V13 "20180524Marmartes&"     
V14 "20180524Marmiercoles&"  
V15 "20180524Marjueves&"     
V16 "20180524MarViernes&" 

There would be no export to txt where you would remove the row and column names.

Could someone give me another better solution?

Thank you.

    
asked by Uko 24.05.2018 в 16:27
source

1 answer

1

A very simple way using base code is the following:

# Para esta solución conviene que los valores sean vectores no data.tables
personas <- c("Luis","Pedro","Mar")
año <- "2018"
mes <-"05"
dia <- "23"
# Acá agregamos un blanco a la lista de días
dia2 <- c("lunes","martes","miercoles","jueves","Viernes", "")

cbind(do.call(paste0, expand.grid(paste0(año, mes, dia), personas, dia2, "&")))

     [,1]                     
 [1,] "20180523Luislunes&"     
 [2,] "20180523Pedrolunes&"    
 [3,] "20180523Marlunes&"      
 [4,] "20180523Luismartes&"    
 [5,] "20180523Pedromartes&"   
 [6,] "20180523Marmartes&"     
 [7,] "20180523Luismiercoles&" 
 [8,] "20180523Pedromiercoles&"
 [9,] "20180523Marmiercoles&"  
[10,] "20180523Luisjueves&"    
[11,] "20180523Pedrojueves&"   
[12,] "20180523Marjueves&"     
[13,] "20180523LuisViernes&"   
[14,] "20180523PedroViernes&"  
[15,] "20180523MarViernes&"    
[16,] "20180523Luis&"          
[17,] "20180523Pedro&"         
[18,] "20180523Mar&"        
  • We use expand.grid() that generates us the combination of all the values passed as parameters
  • Then with do.call() we execute a paste0 of columns
  • Finally, with cbind() we convert everything in a single column matrix

Note, if you still need to use the data.table I recommend you make unlist() of them before passing them as a parameter to expand.grid() :

cbind(do.call(paste0, expand.grid(unlist(personas), paste0(año, mes, dia), unlist(dia2), "&")))

If the Order is important

To sort by Date, Person and Day of the week:

personas <- c("Luis","Pedro","Mar")
año <- "2018"
mes <-"05"
dia <- "23"
dia2 <- factor(c("", "lunes","martes","miercoles","jueves","Viernes"), ordered=T)

m <- expand.grid(paste0(año, mes, dia), as.character(personas), as.character(dia2), "&")
cbind(do.call(paste0, m[order(m$Var1,m$Var2,m$Var3),]))
    
answered by 24.05.2018 в 17:11