So I need to create a csv file which needs to be read by a piece of third party software (pxjob). For some reason, it refuses to read my scandinavian characters if I don't use UTF-8-BOM. Since I must also use semicolons as delimiters I tend to use the write_excel_csv-function, since that takes care of both of these problems.
The csv files look like this when they're exported:
"Deltagande kommuner vårdtagare";;;
"Län";"Enhet";"År";"value"
"10 Blekinge län";"Totalt antal kommuner i län";"2015";"5"
"10 Blekinge län";"Antal deltagande kommuner";"2015";"4"
"20 Dalarnas län";"Totalt antal kommuner i län";"2015";"15"
"20 Dalarnas län";"Antal deltagande kommuner";"2015";"4"
"09 Gotlands län";"Totalt antal kommuner i län";"2015";"1"
However, this format causes pxjob to read the file incorrectly:
I have however found a strange workaround. If I open Excel, do a random change, reverse the change and save/overwrite the csv file (as a csv file, still), the file will look like this instead:
Deltagande kommuner vårdtagare;;;
Län;Enhet;År;value
10 Blekinge län;Totalt antal kommuner i län;2015;5
10 Blekinge län;Antal deltagande kommuner;2015;4
20 Dalarnas län;Totalt antal kommuner i län;2015;15
20 Dalarnas län;Antal deltagande kommuner;2015;4
09 Gotlands län;Totalt antal kommuner i län;2015;1
So the double quotes have disapperad. And now the files are read correctly into pxjob:
There's probably a more precise terminology for all of this, but is it possible to create the csv file in this format directly from R, and preferrably using the write_excel_csv function (or something equivalent)?
Repex:
structure(list(Län = c("10 Blekinge län", "10 Blekinge län",
"20 Dalarnas län", "20 Dalarnas län", "09 Gotlands län", "09 Gotlands län"
), Enhet = c("Totalt antal kommuner i län", "Antal deltagande kommuner",
"Totalt antal kommuner i län", "Antal deltagande kommuner",
"Totalt antal kommuner i län", "Antal deltagande kommuner"),
År = c(2015, 2015, 2015, 2015, 2015, 2015), value = c(5,
4, 15, 4, 1, 0)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
UPDATE:
If I use the following export statement:
write_excel_csv2(as.data.frame(df2),
paste0("G:\\Data\\Folkhälsodata\\_3b px-filer (arbetsmaterial2023)\\Filer för Pxjob\\In_headers\\",fillista[i]),col_names=FALSE, na="", quote = c("none"))
I end up with a file looking like this:
Deltagande kommuner vårdtagare;;;
Län;Enhet;År;value
10 Blekinge län;Totalt antal kommuner i län;2015;5
10 Blekinge län;Antal deltagande kommuner;2015;4
20 Dalarnas län;Totalt antal kommuner i län;2015;15
20 Dalarnas län;Antal deltagande kommuner;2015;4
But with a file looking like this after I run it through pxjob:
(so the first character in the variable name of the first observation has been ignored, thus creating a new variable)