read.transpose: Read a data table in transpose form

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/read.transpose.R

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 tables published by the Bureau of Economic Analysis of the United States Department of Commerce.

Usage

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

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 charactor.

na.strings

character string(s) that translate into NA

...

optional arguments for strsplit

Details

1. txt <- readLines(file)

2. Split into fields.

3. Identifiy 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

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.

Author(s)

Spencer Graves

References

Table 6.16. Income and employment by industry in the National Income and Product Account tables published by the Bureau of Economic Analysis of the United States Department of Commerce. To get this table from www.bea.gov, under "U.S. Economic Accounts", first select "Corporate Profits" under "National". Then next to "Interactive Tables", select, "National Income and Product Accounts Tables". From there, select "Begin using the data...". Under "Section 6 - income and employment by industry", select each of the tables starting "Table 6.16". 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.

See Also

read.table readLines strsplit

Examples

1
2
3
4
5
6
7
8
9
#  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)

Example output

Attaching package:EcfunThe following object is masked frompackage:base:

    sign

[1] "/usr/lib/R/site-library/Ecdat/demoFiles/NIPA6.16A20090820.csv"
[2] "/usr/lib/R/site-library/Ecdat/demoFiles/NIPA6.16B20080817.csv"
[3] "/usr/lib/R/site-library/Ecdat/demoFiles/NIPA6.16C20080817.csv"
[4] "/usr/lib/R/site-library/Ecdat/demoFiles/NIPA6.16D20130130.csv"
 num [1:15, 1:34] 1998 1999 2000 2001 2002 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:34] "\U3e30613c" "    Corporate profits with inventory valuation and capital consumption adjustments" "Domestic industries" "  Financial /1/" ...
 - attr(*, "headers")= chr [1:4] "Table 6.16D. Corporate Profits by Industry" "[Billions of dollars] " "Bureau of Economic Analysis " "Last Revised on: January 30, 2013 - Next Release Date February 28, 2013"
 - attr(*, "footers")= chr [1:7] "Legend / Footnotes:" "1. Consists of finance and insurance and bank and other holding companies." "2. Consists of credit intermediation and related activities; securities, commodity contracts, and other financi"| __truncated__ "3. Consists of wood products; nonmetallic mineral products; primary metals; other transportation equipment; fur"| __truncated__ ...
 - attr(*, "summary")= Named int [1:3] 4 7 34
  ..- attr(*, "names")= chr [1:3] "headers" "footers" "data"

Ecfun documentation built on May 2, 2019, 6:53 p.m.