Reads data from a tabular file

Share:

Description

Reads data from a tabular file or a set of such files.

Usage

1
2
## Default S3 method:
readDataFrame(filename, path=NULL, ...)

Arguments

filename, path

character vector specifying one or more files to be read.

...

Additional arguments passed to either (i) readDataFrame for class TabularTextFile, or (ii) readDataFrame for class TabularTextFileSet, depending on whether one or multiple files are read.

Details

When reading multiple files at once, first each file is read into a data.frame, and then these data.frames are (by default) merged into one data.frame using rbind(). This requires that the same set of columns are read for each file. Which columns to read can be controlled by specifying their names in argument colClasses. To change how the data.frames are merged, use argument combineBy. For more information, follow the help on the above to readDataFrame() help links.

Value

Returns a data.frame.

Author(s)

Henrik Bengtsson

See Also

read.table. For further details, see classes TabularTextFile and TabularTextFileSet.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
path <- system.file("exData/dataSetA,original", package="R.filesets")

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example: Standard tab-delimited file with header comments
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pathname <- file.path(path, "fileA,20100112.dat")

# Read all data
df <- readDataFrame(pathname)
print(df)

# Read columns 'x', 'y', and 'char'
df <- readDataFrame(pathname, colClasses=c("(x|y)"="integer", "char"="character"))
print(df)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example: Tab-delimited file with header comments but
#          also two garbage at the very beginning
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
pathname <- file.path(path, "fileA,20130116.datx")

# Explicitly skip the two rows
df <- readDataFrame(pathname, skip=2)
print(df)


# Skip until the first data row
df <- readDataFrame(pathname, skip="^x")
print(df)


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Example: Tab-delimited file without column header
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
path <- system.file("exData/dataSetB", package="R.filesets")
pathname <- file.path(path, "fileF,noHeader.dat")

# Incorrectly assuming column header
df <- readDataFrame(pathname)
print(df)

# No column header
df <- readDataFrame(pathname, header=FALSE)
print(df)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.