Learn R Programming

plotfunctions (version 1.4)

convertFile: Replacing separators (for example, decimal and thousand separators).

Description

Replacing separators (for example, decimal and thousand separators).

Usage

convertFile(
  filename,
  symbol1 = NULL,
  symbol2 = NULL,
  newsymbol1 = "",
  newsymbol2 = "",
  sep = ";",
  newsep = NULL,
  header = TRUE,
  columns = NULL,
  outputfile = gsub("^(.*)(\\.)([^\\.]*)$", "\\1_new.\\3", filename),
  fixed.s1 = TRUE,
  fixed.s2 = TRUE,
  fixed.sep = TRUE,
  ...
)

Arguments

filename

String: filename (including path if necessary) of input file.

symbol1

String: symbol to replace by newsymbol1, for example decimal separator.

symbol2

String: second symbol to replace by newsymbol2, for example thousand separator.

newsymbol1

String: symbol to replace symbol1.

newsymbol2

String: symbol to replace symbol2.

sep

String: column separator. Could be also used to replace symbols in the header and data by newsep, regardless of columns.

newsep

String: symbol to replace sep. Only possible when columns is set to NULL.

header

Logical: whether or not there is header line. symbol1 and symbol2 are not replaced in the header line. Default set to TRUE.

columns

Vector with numerical values: indices of columns in which symbols need to be replaced.

outputfile

String: name of outputfile.

fixed.s1

Logical: whether or not to treat symbol1 as fixed text instead of regular expression. Default is set to TRUE (no regular expression).

fixed.s2

Logical: whether or not to treat symbol2 as fixed text instead of regular expression. Default is set to TRUE (no regular expression).

fixed.sep

Logical: whether or not to treat sep as fixed text instead of regular expression. Default is set to TRUE (no regular expression).

...

Additional parameters for read.table and write.table.

Examples

Run this code
# NOT RUN {
# normally, the function call would look something like this:
convertFile('example1.csv', symbol1=',', symbol2='.', sep='\t', 
    newsymbol1='.', newsymbol2='')
# But as we are not sure that the file example1.csv is available,
# we need to do something a little more complicated to point to 
# the file 'example1.csv' that comes with the package:

# finding one of the example files from the package:
file1 <- system.file('extdata', 'example1.csv', package = 'plotfunctions')

# example 1: 
system.time({
    convertFile(file1, symbol1=',', symbol2='.', 
    newsymbol1='.', newsymbol2='', outputfile='example1_new.csv')
})
# example 2: type 'yes' to overwrite the previous output file, 
# or specify a different filename in outputfile.
system.time({
    convertFile(file1, symbol1=',', symbol2='.', sep='\t', 
    newsymbol1='.', newsymbol2='', columns=1:2, outputfile='example1_new.csv')
})
# Example 1 takes less  time, as it does not use read.table, 
# but just reads the file as text lines. However, the column 
# version could be useful when symbols should be replaced only 
# in specific columns.
# Note that Example 2 writes the output with quotes, but this is 
# not a problem for read.table:
dat <- read.table('example1_new.csv', header=TRUE, sep='\t', 
    stringsAsFactors=FALSE)
# }

Run the code above in your browser using DataLab