Learn R Programming

expss (version 0.7.1)

write_labels: Write data with labels to file in R code or in SPSS syntax.

Description

write_labelled_* functions write data in the CSV format and file with R code/SPSS syntax for labelling data. SPSS syntax also contains code for reading data in SPSS. write_labelled_* doesn't save rownames of data.frame. write_labels_* functions write R code/SPSS syntax for labelling data. It allows to extract labels from *.sav files that come without accompanying syntax. read_labelled_csv reads data file in CSV format and apply labels from accompanying file with R code. *_csv2 write/read data with semicolon separator and comma as decimal delimiter. *_tab/*_tab2 write/read data with tab separator and "."/"," as decimal delimiter.

Usage

write_labels(x, filename, fileEncoding = "")

read_labelled_csv(filename, fileEncoding = "", encoding = "unknown", sep = ",", dec = ".", ...)

read_labelled_csv2(filename, fileEncoding = "", encoding = "unknown", sep = ";", dec = ",", ...)

read_labelled_tab(filename, fileEncoding = "", encoding = "unknown", sep = "\t", dec = ".", ...)

read_labelled_tab2(filename, fileEncoding = "", encoding = "unknown", sep = "\t", dec = ",", ...)

write_labelled_csv(x, filename, fileEncoding = "", sep = ",", dec = ".", ...)

write_labelled_csv2(x, filename, fileEncoding = "", sep = ";", dec = ",", ...)

write_labelled_tab(x, filename, fileEncoding = "", sep = "\t", dec = ".", ...)

write_labelled_tab2(x, filename, fileEncoding = "", sep = "\t", dec = ",", ...)

write_labelled_spss(x, filename, fileEncoding = "", ...)

write_labels_spss(x, filename)

Arguments

x

data.frame to be written/data.frame whose labels to be written

filename

the name of the file which the data are to be read from/write to.

fileEncoding

character string: if non-empty declares the encoding to be used on a file (not a connection) so the character data can be re-encoded as they are written. Used for writing dictionary. See file.

encoding

default is "unknown". Other possible options are "UTF-8" and "Latin-1". Note: it is not used to re-encode the input, rather enables handling of encoded strings in their native encoding. Used for writing data file. See fread.

sep

the field separator string. Values within each row of x are separated by this string.

dec

the string to use for decimal points in numeric or complex columns: must be a single character.

...

additional arguments for fwrite/fread

Value

Functions for writing invisibly return NULL. Functions for reading return labelled data.frame.

Examples

Run this code
# NOT RUN {
data(mtcars)
mtcars = modify(mtcars,{
                var_lab(mpg) = "Miles/(US) gallon"
                var_lab(cyl) = "Number of cylinders"
                var_lab(disp) = "Displacement (cu.in.)"
                var_lab(hp) = "Gross horsepower"
                var_lab(drat) = "Rear axle ratio"
                var_lab(wt) = "Weight (lb/1000)"
                var_lab(qsec) = "1/4 mile time"
                var_lab(vs) = "Engine"
                val_lab(vs) = c("V-engine" = 0, 
                                "Straight engine" = 1) 
                var_lab(am) = "Transmission"
                val_lab(am) = c(automatic = 0, 
                                manual=1)
                var_lab(gear) = "Number of forward gears"
                var_lab(carb) = "Number of carburetors"
})

# to R code
# rownames are not preserved
write_labelled_csv(mtcars, "mtcars.csv")
new_mtcars = read_labelled_csv("mtcars.csv")

# to SPSS syntax
write_labelled_spss(mtcars, "mtcars.csv")

# }

Run the code above in your browser using DataLab