Wednesday 24 June 2015

R : read.table : Read data from file

“read.table” method is used to read data from a file in table format.

read.table(file, header = FALSE, sep = "", quote = "\"'",
           dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
           row.names, col.names, as.is = !stringsAsFactors,
           na.strings = "NA", colClasses = NA, nrows = -1,
           skip = 0, check.names = TRUE, fill = !blank.lines.skip,
           strip.white = FALSE, blank.lines.skip = TRUE,
           comment.char = "#",
           allowEscapes = FALSE, flush = FALSE,
           stringsAsFactors = default.stringsAsFactors(),
           fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)

Parameter
Description
file
The name of the file to read data from. Each line of the file corresponds to a row in table.
header
Header indicates whether the file contains names of the variables as firstline.
sep
Field separator character, It can be white space, tab, comma (,) etc.,
quote
The set of quoting characters. To disable quoting, use quote = ""
dec
The character used in the file for decimal points.
numerals
Indicate how to convert numbers
row.names
A vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.
col.names
A vector of optional names for the variables. The default is to use "V" followed by the column number.
as.is
By defult, read.table converts character variables to factors. You can control this behavior by using as.is.
na.strings
A character vector of strings which are to be interpreted as NA values. Blank fields are also considered to be missing values in logical, integer, numeric and complex fields.
colClasses
A vector of classes to be assumed for the columns.
nrows
Maximum number of rows to read.
skip
Skip number of lines before reading data.
check.names
If it is set to TRUE, then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names.
fill
If it is set to TRUE, blank fields are added, if rown are unequal length.
strip.white
used for the stripping of leading and trailing white space from unquoted character fields
blank.lines.skip
If it is set to TRUE, then blank lines in the input are ignored.

comment.char
It is a character vector of length 1, used to specify comment symbol.

Let’s say employee.txt contains following data.

"firstName" "lastName"
"Hari Krishna" "Gurram"
"Joel" "Chelli"
"Rama Krishna" "Gurram"

"Sudheer" "Ganji"
> table2 <- read.table("employee.txt")
> 
> table2
            V1       V2
1    firstName lastName
2 Hari Krishna   Gurram
3         Joel   Chelli
4 Rama Krishna   Gurram
5      Sudheer    Ganji
> 
> table2 <- read.table("employee.txt", skip=2)
> 
> table2
            V1     V2
1         Joel Chelli
2 Rama Krishna Gurram
3      Sudheer  Ganji



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment