1

I have a .csv file that contains this:

84,62,175,235,374,221,292,375,430,497,0,0,736,0,0,0
85,62,298,337,149,181,247,424,228,383,0,0,1657,0,0,0
87,62,198,181,347,355,423,479,403,494,0,0,261,0,0,0
88,62,289,202,422,310,388,430,498,604,0,0,259,0,0,0
122,41,276,231,247,294,402,463,383,461,0,0,87,0,0,0

I have another .csv file that contains this:

10848,0,0,0,272826,84
10849,0,0,0,272806,85
10850,0,1,0,272812,85
10851,1,0,0,440831,85
10870,0,0,0,271567,122
10871,0,1,0,438415,122

Using R, how can I merge these together (by first column in first table and last column in second) and add quotes to make it look nicer.

Many thanks.

Jstation
  • 407
  • 4
  • 14

1 Answers1

1

This is a possible way to go, just adapt it to your case:

df<-c('33','429:301:255:38','450692,450693,450694,450695')
df[1]<-paste('[',shQuote(df[1]),']')
df[2]<-paste(' = {[',shQuote(df[2]),']')
df[3]<-paste(', [',shQuote(df[3]),']}')
dff<-paste(df[1],df[2],df[3])
library("openxlsx") 
setwd("C:\\temp")
wb<-createWorkbook()
addWorksheet(wb, sheetName = "test")
writeData(wb,dff,sheet = "test", rowNames = FALSE)
name <- paste("mydata_",Sys.Date(),".xlsx", sep="")
saveWorkbook(wb,name, overwrite = TRUE)
ecp
  • 319
  • 1
  • 6
  • 18
  • You have to adapt the code, just do the same with the rest of the vector. – ecp May 31 '19 at 11:10
  • I updated answer, 1st and 2nd are merged with formatted output desired, just update the code with the rest (it is exactly the same procedure). – ecp May 31 '19 at 11:13
  • Well, here there are more than one question. read https://stackoverflow.com/questions/31429736/how-to-join-data-from-2-different-csv-files-in-r for merging, concatenate them with paste() as in the solution and apply the solution given. This should work. – ecp May 31 '19 at 12:33