write.data: Write Data File in Table Format, SPSS, Excel, or Stata DTA...

View source: R/write.data.R

write.dataR Documentation

Write Data File in Table Format, SPSS, Excel, or Stata DTA File

Description

This function writes a (1) data file in CSV (.csv), DAT (.dat), or TXT (.txt) format using the fwrite function from the data.table package, (2) SPSS file (.sav) using the write.sav function, (3) Excel file (.xlsx) using the write.xlsx function, or a (4) Stata DTA file (.dta) using the write.dta function in the misty package. Note that the function write.data uses "," for decimal point and a semicolon ";" for the separator, while the function write.data1 uses "." for decimal point and a comma "," for the separator when writing a CSV file.

Usage

write.data(x, file = "Data.csv", sep = ";", dec = ",", na = "",
           row.names = FALSE, col.names = TRUE, check = TRUE, ...)

write.data1(x, file = "Data.csv", sep = ",", dec = ".", na = "",
            row.names = FALSE, col.names = TRUE, check = TRUE, ...)

Arguments

x

a matrix or data frame to be written.

file

a character string indicating the name of the data file with the file extension .csv, .dat, .txt, .sav, .xlsx, or .dta. Note that the function will select an appropriate write-function depending on the file extension.

sep

a character string indicating the field separator, i.e., string for the delimiter. By default, the write.data function uses a semicolon ";", while the function write.data1 function uses a comma "," for writing a CSV, DAT, or TXT data file

dec

a character string indicating the decimal separator, i.e., string for decimal points. By default, the write.data function uses a comma ",", while the function write.data1 function uses a decimal point "." for writing a CSV, DAT, or TXT data file.

na

a character string to use for missing values in the data. By default, a blank string "" is used.

row.names

logical: if FALSE, row names are written.

col.names

logical: if TRUE (default), column names are written.

check

logical: if TRUE (default), argument specification is checked.

...

additional arguments to pass to the fwrite write.sav, write.xlsx, or write.dta function, see Arguments section in the help pages.

Details

Comma-Separated Values (CSV) File

The function write.data writes CSV files based on the Excel convention for CSV files in some Western European locales by default, i.e., ";" as delimiter and "," for decimal points. Depending on the language setting of the operating system of the computer, the arguments sep and dec need to be specified to "," and "." (see Example 1b). Alternatively, the function write.data1 that uses "," as delimiter and "." for decimal points by default can be used (see Example 1c).

Author(s)

Takuya Yanagida

References

Barrett, T., Dowle, M., Srinivasan, A., Gorecki, J., Chirico, M., Hocking, T., & Schwendinger, B. (2024). data.table: Extension of 'data.frame'. R package version 1.16.0. https://CRAN.R-project.org/package=data.table

Jeroen O. (2021). writexl: Export Data Frames to Excel 'xlsx' Format. R package version 1.4.0. https://CRAN.R-project.org/package=writexl

Wickham H, Miller E, Smith D (2023). haven: Import and Export 'SPSS', 'Stata' and 'SAS' Files. R package version 2.5.3. https://CRAN.R-project.org/package=haven

See Also

read.data, read.sav, write.sav, write.xlsx, read.dta, write.dta, read.mplus, write.mplus

Examples

## Not run: 
# Example 1a: Write CSV data file, European format
write.data(mtcars, "European_CSV_Data.csv")

# Example 1b: Write CSV data file, American format
write.data(mtcars, "American_CSV_Data.csv", sep = ",", dec = ".")

# Example 1c: Write CSV data file, American format
write.data1(mtcars)

# Example 2: Write SPSS data file
write.data(mtcars, "SPSS_Data.sav")

# Example 3:  Write Excel data file
write.data(mtcars, "Excel_Data.xlsx")

# Example 4:  Write Stata data file
write.data(mtcars, "Stata_Data.dta")

## End(Not run)

misty documentation built on Aug. 18, 2025, 5:16 p.m.

Related to write.data in misty...