Learn R Programming

Ecfun (version 0.3-2)

read.transpose: Read a data table in transpose form

Description

Read a text (e.g., csv) file, find rows with more than 3 sep characters. Parse the initial contiguous block of those into a matrix. Add attributes headers, footers, and a summary.

The initial application for this function is to read "Table 6.16. Income and employment by industry" in the National Income and Product Account (NIPA) tables published by the Bureau of Economic Analysis (BEA) of the United States Department of Commerce.

Usage

read.transpose(file, header=TRUE, sep=',',
               na.strings='---', ...)

Value

A matrix of the transpose of the rows with the max number of fields with attributes

headers, footers,

other, and summary. If this matrix can be coerced to numeric with no

NAs, it will be. Otherwise, it will be left as character.

Arguments

file

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

header

Logical: Is the second column of the identified data matrix to be interpreted as variable names?

sep

The field space separator character.

na.strings

character string(s) that translate into NA

...

optional arguments for strsplit

Author

Spencer Graves

Details

1. txt <- readLines(file)

2. Split into fields.

3. Identify headers, Data, footers.

4. Recombine the second component of each Data row if necessary so all have the same number of fields.

5. Extract variable names

6. Numbers?

7. return the transpose

References

Table 6.16. Income and employment by industry in the National Income and Product Account (NIPA) tables published by the Bureau of Economic Analysis (BEA) of the United States Department of Commerce. As of February 2013, there were 4 such tables available: Table 6.16A, 6.16B, 6.16C and 6.16D. Each of the last three are available in annual and quarterly summaries. The USFinanceIndustry data combined the first 4 rows of the 4 annual summary tables.

NOTE: The structure of the BEA web site seems to have changes between 2013 and 2022. As of 2022-07-01 it does not seem easy to find these tables at the BEA website.

Line 5 in the sample tables saved in 2013 contained "a non-breaking space in Latin-1", which was not a valid code in UTF-8 and was rejected by a development version of R. Since it wasn't easy to update those tables, the "non-breaking spaces" were replaced with " ".

See Also

Examples

Run this code
#  Find demoFiles/*.csv
demoDir <- system.file('demoFiles', package='Ecdat')
(demoCsv <- dir(demoDir, pattern='csv$', full.names=TRUE))

# Use the fourth example
# to ensure the code will handle commas in a name
# and NAs
nipa6.16D <- read.transpose(demoCsv[4])
str(nipa6.16D)

Run the code above in your browser using DataLab