convertCsvFile | R Documentation |
This function allows you to change the format (such as column delimiter,
decimal character) of a CSV file. It uses read.table
to read a
CSV file and write.table
to rewrite the file with modified
format to a new file. All arguments of read.table
and
write.table
are supported. Arguments that are provided by both
functions appear as two arguments <argument_name>_in
and
<argument_name>_out
in this function.
convertCsvFile(
file_in,
sep_in = formals(utils::read.table)$sep,
sep_out = sep_in,
dec_in = formals(utils::read.table)$dec,
dec_out = dec_in,
file_out = NULL,
header = TRUE,
quote_in = formals(utils::read.table)$quote,
quote_out = formals(utils::write.table)$quote,
row.names_in = formals(utils::read.table)$row.names,
col.names_in = formals(utils::read.table)$col.names,
row.names_out = FALSE,
col.names_out = TRUE,
fileEncoding_in = formals(utils::read.table)$fileEncoding,
fileEncoding_out = fileEncoding_in,
dbg = TRUE,
...
)
file_in |
path to input file |
sep_in |
column separator in input file |
sep_out |
column separator in output file |
dec_in |
decimal character in input file |
dec_out |
decimal character inoutput file |
file_out |
path to output file |
header |
passed to |
quote_in |
passed as |
quote_out |
passed as |
row.names_in |
passed as |
col.names_in |
passed as |
row.names_out |
passed as |
col.names_out |
passed as |
fileEncoding_in |
passed as |
fileEncoding_out |
passed as |
dbg |
if |
... |
further arguments passed to either |
path to the created CSV file
# Write the iris dataset to a temporary file with "," as column separator
csv_in <- tempfile(fileext = ".csv")
write.table(iris, csv_in, row.names = FALSE)
# Review the first lines of the file
catLines(readLines(csv_in, 6))
# Convert the column separator (from " " which was the default) to ";"
csv_out <- convertCsvFile(csv_in, sep_out = ";")
# Review the result
catLines(readLines(csv_out, 6))
# Delete the file so that it can be recreated
unlink(csv_out)
# Convert the column separator and the decimal character
csv_out <- convertCsvFile(csv_in, sep_out = ";", dec_out = ",")
# Review the result
catLines(readLines(csv_out, 6))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.