Learn R Programming

psych (version 1.7.8)

read.file: Shortcuts for reading from the clipboard or a file

Description

Input from a variety of sources may be read. data.frames may be read from files with suffixes of .txt, .text, .TXT, .dat, .data, .csv, .rds, rda, .xpt, or .sav (i.e., data from SPSS sav files may be easily read.) Data exported by JMP or EXCEL in the csv format are also able to be read. Fixed Width Files saved in .txt mode may be read if widths is specified. Files saved with writeRDS have suffixes of .rds or Rds, and are read using readRDS. Files associated with objects with suffixes .rda and .Rda are loaded. The default values for read.spss are adjusted for more standard input from SPSS files. Input from the clipboard is easy but a bit obscure, particularly for Mac users. This is just an easier way to do so. Data may be copied to the clipboard from Excel spreadsheets, csv files, or fixed width formatted files and then into a data.frame. Data may also be read from lower (or upper) triangular matrices and filled out to square matrices.

Usage

read.file(f=NULL,header=TRUE,use.value.labels=FALSE,to.data.frame=TRUE,sep=",",
widths=NULL, ...)
 #for .txt, .text, TXT, .csv, .sav, .xpt, XPT,  R, r, Rds, .rds, or .rda, 
# Rda, RData, Rdata, files

read.clipboard(header = TRUE, ...) #assumes headers and tab or space delimited read.clipboard.csv(header=TRUE,sep=',',...) #assumes headers and comma delimited read.clipboard.tab(header=TRUE,sep='\t',...) #assumes headers and tab delimited #read in a matrix given the lower off diagonal read.clipboard.lower(diag=TRUE,names=FALSE,...) read.clipboard.upper(diag=TRUE,names=FALSE,...)

#read in data using a fixed format width (see read.fwf for instructions) read.clipboard.fwf(header=FALSE,widths=rep(1,10),...) read.https(filename,header=TRUE)

read.file.csv(f=NULL,header=TRUE,...) write.file(x,f=NULL,row.names=FALSE,...) write.file.csv(x,f=NULL,row.names=FALSE,...)

Arguments

header

Does the first row have variable labels (generally assumed to be TRUE).

sep

What is the designated separater between data fields? For typical csv files, this will be a comma, but if commas designate decimals, then a ; can be used to designate different records.

diag

for upper or lower triangular matrices, is the diagonal specified or not

names

for read.clipboard.lower or upper, are colnames in the the first column

widths

how wide are the columns in fixed width input. The default is to read 10 columns of size 1.

filename

Name or address of remote https file to read.

Other parameters to pass to read

f

A file name to read. If omitted, file.choose is called to dynamically get the file name.

x

The data frame or matrix to write to f

row.names

Should the output file include the rownames?

to.data.frame

Should the spss input be converted to a data frame?

use.value.labels

Should the SPSS input values be converted to numeric?

Value

the contents of the file to be read or of the clipboard.

Details

A typical session of R might involve data stored in text files, generated online, etc. Although it is easy to just read from a file (particularly if using file.choose, copying from the file to the clipboard and then reading from the clipboard is also very convenient (and somewhat more intuitive to the naive user). This is particularly convenient when copying from a text book or article and just moving a section of text into R.)

The read.file function combines the file.choose and either read.table, read.fwf, read.spss or read.xport(from foreign) or load or readRDS commands. By examining the file suffix, it chooses the appropriate way to read. For more complicated file structures, see the foreign package. For even more complicated file structures, see the rio package.

Note that read.file assumes by default that the first row has column labels (header =TRUE). If this is not true, then make sure to specify header = FALSE. If the file is fixed width, the assumption is that it does not have a header field. In the unlikely case that a fwf file does have a header, then you probably should try fn <- file.choose() and then my.data <- read.fwf(fn,header=TRUE,widths= widths)

Currently supported input formats are

.sav SPSS.sav files
.csv A comma separated file (e.g. from Excel or Qualtrics)
.txt A typical text file
.TXT A typical text file
.text A typical text file
.data A data file
.dat A data file
.rds A R data file
.Rds A R data file (created by a write)
.Rda A R data structure (created using save)
.rda A R data structure (created using save)
.RData A R data structure (created using save)
.rdata A R data structure (created using save)
.R A R data structure created using dput
.r A R data structure created using dput
.xpt A SAS data file in xport format
.XPT A SAS data file in XPORT format

The foreign function read.spss is used to read SPSS .sav files using the most common options. Just as read.spss issues various warnings, so does read.file. In general, these can be ignored. For more detailed information about using read.spss, see the help pages in the foreign package.

If you have a file written by JMP, you must first export to a csv or text file.

The write.file function combines the file.choose and either write.table or saveRDS. By examining the file suffix, it chooses the appropriate way to write. For more complicated file structures, see the foreign package, or the save function in R Base.

Currently supported output formats are

.csv A comma separated file (e.g. for reading into Excel)
.txt A typical text file
.text A typical text file
.rds A R data file
.Rds A R data file (created by a write)
.Rda A R data structure (created using save)
.rda A R data structure (created using save)
.R A R data structure created using dput
.r A R data structure created using dput

read.clipboard was based upon a suggestion by Ken Knoblauch to the R-help listserve.

If the input file that was copied into the clipboard was an Excel file with blanks for missing data, then read.clipboard.tab() will correctly replace the blanks with NAs. Similarly for a csv file with blank entries, read.clipboard.csv will replace empty fields with NA.

read.clipboard.lower and read.clipboard.upper are adapted from John Fox's read.moments function in the sem package. They will read a lower (or upper) triangular matrix from the clipboard and return a full, symmetric matrix for use by factanal, factor.pa , ICLUST , etc. If the diagonal is false, it will be replaced by 1.0s. These two function were added to allow easy reading of examples from various texts and manuscripts with just triangular output.

Many articles will report lower triangular matrices with variable labels in the first column. read.clipboard.lower will handle this case. Names must be in the first column if names=TRUE is specified.

Other articles will report upper triangular matrices with variable labels in the first row. read.clipboard.upper will handle this. Note that labels in the first column will not work for read.clipboard.upper. The names, if present, must be in the first row.

read.clipboard.fwf will read fixed format files from the clipboard. It includes a patch to read.fwf which will not read from the clipboard or from remote file. See read.fwf for documentation of how to specify the widths.

Examples

Run this code
# NOT RUN {
#Because these are dynamic functions, they need to be run interactively and 
# can not be shown as examples.

#my.data <- read.file()   #search the directory for a file and then read it.
#the example data set from read.delim in the readr package to read a remote csv file
#my.data  <-read.file("https://github.com/tidyverse/readr/raw/master/inst/extdata/mtcars.csv")
#my.data <- read.clipboad()
#my.data <- read.clipboard.csv()
#my.data <- read.clipboad(header=FALSE)
#my.matrix <- read.clipboard.lower()
# }

Run the code above in your browser using DataLab