write2csv: Write a data frame to a CSV file

View source: R/write2csv.R

write2csvR Documentation

Write a data frame to a CSV file

Description

Write a data frame to a CSV file in a special form, with info about the number of rows and columns.

Usage

write2csv(
  df,
  filename,
  comment = "",
  sep = ",",
  comment.char = "#",
  row.names = NULL,
  overwrite = FALSE
)

Arguments

df

A data frame (or matrix)

filename

File name to write

comment

Comment to place on the first line

sep

Field separator

comment.char

Character to use to initiate the comment lines

row.names

If NA or NULL (the default), row names are not included in the output file. Otherwise, the row names are included as the first column of the output, and this is taken to be the name for that column.

overwrite

If TRUE, overwrite file if it exists. If FALSE (the default) and the file exists, stop with an error.

Details

If the file already exists, the function will refuse to write over it.

The file will include comments at the top, using # as a comment character, including the number of rows (not including the header) and the number of columns.

By default, row names are not included. But with the option row.names provided as a character string, they are added as an initial column, with the value of this argument defining the name for that column. If a column with that name already exists, the function halts with an error.

Value

None.

Examples

nr <- 10
nc <- 5
x <- data.frame(id=paste0("ind", 1:nr),
                matrix(rnorm(nr*nc), ncol=nc))
colnames(x)[1:nc + 1] <- paste0("col", 1:nc)

testfile <- file.path(tempdir(), "tmpfile.csv")
write2csv(x, testfile, "A file created by write2csv")

# Remove the file, to clean up temporary directory
unlink(testfile)

qtl2convert documentation built on July 11, 2022, 5:08 p.m.