Learn R Programming

lessR (version 1.9.8)

rad: Read, Attach and Display Contents of a csv File

Description

Reads the contents of the specified csv data file into an R dataframe called mydata. Identifies the file by either browsing for the file on the local computer system with rad(), or as indicated by a path name or a web URL. Then attaches and lists the first and last three rows of data as well as the variable names and the dimensions of the resulting data frame. In addition, perform an analysis of missing data, listing the number of missing values for each variable and for each observation.

Usage

rad(ref=NULL, display=TRUE, show.R=FALSE, no.attach=FALSE, 
         miss.zero=TRUE, miss.matrix=FALSE, 
         format=c("csv", "SPSS"), ...)

Arguments

ref
File reference, either null to specify the default of file.choose() to browse for the csv data file, or a full path name or web URL, included in quotes. A URL begins with http://.
display
The output of each step is displayed at the console, but this option can be turned off.
show.R
Display the R instructions that yielded the lessR output, albeit without the lessR formatting.
no.attach
Off by default, but if TRUE then do not attach the R data frame, mydata by default.
miss.zero
For the missing value analysis, list the variable name or the row name even for values of 0.
miss.matrix
For the missing value analysis, if there is any missing data, list a version of the complete data table with a 0 for a non-missing value and a 1 for a missing value.
format
Format of the data in the file, which by default is a csv file, and as option can be an SPSS sav file.
...
Other parameter values consistent with the usual read.csv function, such as row.names and header.

Details

CREATE csv FILE By default rad reads csv data files. One way to create a csv data file is by entering the data into a text editor. A more structured method is to use a worksheet application such as MS Excel, LibreOffice Calc. Place the variable names in the first row of the worksheet. Each column of the worksheet contains the data for the corresponding variable. Each subsequent row contains the data for a specific observation, such as for a person or a company.

All numeric data in the worksheet should be displayed in the General format, so that the only non-digit character for a numeric data value is a decimal point. The General format removes all dollar signs and commas, for example, leaving only the pure number, stripped of these extra characters which R will not properly read as part of a numeric data value.

To create the csv file from Excel, under the File option, do a Save As and choose the csv format.

MECHANICS Given a csv data file, read the data into an R data frame called mydata with rad, which first invokes one of two R statements. Call with no arguments, as in rad(), to invoke:

mydata <- read.csv(file.choose()) Or, call with non-null ref option, rad("file_reference"), to invoke:

mydata <- read.csv("file_reference") Then, rad invokes the subsequent R statements: attach(mydata, warn.conflicts=FALSE), head(mydata, n=3) and tail(mydata, n=3). Output of these statements is directed to the console. Also listed are the R statements invoked by rad.

Because rad calls the standard R function read.csv, which just provides a wrapper for read.table, the usual options that work with read.table, such as row.names also can be passed through rad.

SPSS DATA To read data in the SPSS .sav format, rad calls the read.spss function from the foreign package. To invoke this option, specify format="SPSS".

MISSING DATA By default, rad provides a list of each variable and each row with the display of the number of associated missing values, indicated by the standard R missing value code NA. When reading the data, R automatically sets any empty numeric and logical values as missing. Null character string values are left as null, unless the R na.strings="" option is invoked. Of course any other valid value can be set to missing as well.

To not list the variable name or row name of variables or rows without missing data, invoke the miss.zero=FALSE option, which can appreciably reduce the amount of output for large data sets. To view the entire data table in terms of 0's and 1's for non-missing and missing data, respectively, invoke the miss.matrix=TRUE option.

See Also

read.csv, attach, head, tail.

Examples

Run this code
# to browse for a csv data file on the computer system, invoke rad with 
# the ref argument empty, which, in turn, invokes read.csv(file.choose()),
# and then automatically invokes the attach, head and tail statements
# rad()

# same as above, but include standard read.csv options to indicate 
#  no variable names in first row of the csv data file 
#   and then provide the names
# also indicate that the first column is an ID field
# rad(header=FALSE, col.names=c("X", "Y"), row.names=1)

# read a csv data file from the web
# then attach and list variable names and some values
#rad("http://web.pdx.edu/~gerbing/data/twogroup.csv")

Run the code above in your browser using DataLab