save.data: R Utilities: Saving/Writing Data Files using 'miceadds'

View source: R/save.data.R

save.dataR Documentation

R Utilities: Saving/Writing Data Files using miceadds

Description

This function is a wrapper function for saving or writing data frames or matrices.

Usage

save.data( data, filename, type="Rdata", path=getwd(), row.names=FALSE, na=NULL,
      suffix=NULL, suffix_space="__", index=FALSE, systime=FALSE, ...)

Arguments

data

Data frame or matrix to be saved

filename

Name of data file

type

The type of file in which the data frame or matrix should be loaded. This can be Rdata (for R binary format, using base::save, csv (using utils::write.csv2), csv (using utils::write.csv), table (using utils::write.table), sav (using sjlabelled::write_spss), RDS (using saveRDS). type can also be a vector if the data frame should be saved in multiple formats.

path

Directory from which the dataset should be loaded

row.names

Optional logical indicating whether row names should be included in saved csv or csv2 files.

na

Missing value handling. The default is "" for type="csv" and type="csv2" and is "." for type="table".

suffix

Optional suffix in file name.

suffix_space

Optional place holder if a suffix is used.

index

Optional logical indicating whether an index should be included in the first column using the function index.dataframe.

systime

If index=TRUE, this optional logical indicates whether a time stamp should be included in the second column.

...

Further arguments to be passed to save, write.csv2, write.csv, write.table or sjlabelled::write_spss.

See Also

See load.Rdata and load.data for saving/writing R data frames.

Examples

## Not run: 
#############################################################################
# EXAMPLE 1: Save dataset data.ma01
#############################################################################

#*** use data.ma01 as an example for writing data files using save.data
data(data.ma01)
dat <- data.ma01

# set a working directory
pf2 <- "P:/ARb/temp_miceadds"

# save data in Rdata format
miceadds::save.data( dat, filename="ma01data", type="Rdata", path=pf2)

# save data in table format without row and column names
miceadds::save.data( dat, filename="ma01data", type="table", path=pf2,
            row.names=FALSE, na=".", col.names=FALSE)

# save data in csv2 format, including time stamp in file name
# and row index and time stamp in saved data
miceadds::save.data( dat, filename="ma01data", type="csv2", path=pf2,
            row.names=FALSE, na="", suffix=systime()[5],
            index=TRUE, systime=TRUE )

# save data in sav format
miceadds::save.data( dat, filename="ma02data", type="sav",  path=pf2 )

# save data file in different formats
types <- c("Rdata", "csv2", "sav")
sapply( types, FUN=function(type){
    miceadds::save.data( dat, filename="ma02data", type=type,  path=pf2,
               suffix=miceadds::systime()[3], row.names=TRUE  )
                                    } )

# save data frame in multiple file formats (sav, table and csv2)
miceadds::save.data( dat, filename="ma03data", type=c("sav","table","csv2"),  path=pf2,
            suffix=miceadds::systime()[7]  )

## End(Not run)

miceadds documentation built on May 29, 2024, 11:05 a.m.