Questions tagged [read.table]

read.table is basic R function which reads a file in table format and creates a data frame from it, with cases corresponding to lines and variables to fields in the file.

read.table is basic R function which reads a file in table format and creates a data frame from it, with cases corresponding to lines and variables to fields in the file. It is the most convenient way to read in a rectangular grid of data.

Because of the many possibilities, there are several other functions that call read.table but change a group of default arguments. Convenience functions read.csv and read.delim provide arguments to read.table appropriate for CSV and tab-delimited files exported from spreadsheets in English-speaking locales. The variations read.csv2 and read.delim2 are appropriate for use in those locales where the comma is used for the decimal point and (for read.csv2) for spreadsheets which use semicolons to separate fields.

See also The R Data Import/Export manual.

567 questions
23
votes
5 answers

What does the "More Columns than Column Names" error mean?

I'm trying to read in a .csv file from the IRS and it doesn't appear to be formatted in any weird way. I'm using the read.table() function, which I have used several times in the past but it isn't working this time; instead, I get this…
user3084629
  • 445
  • 2
  • 4
  • 7
21
votes
1 answer

Reading text files using read.table

I have a text file with an id and name column, and I'm trying to read it into a data frame in R: d = read.table("foobar.txt", sep="\t") But for some reason, a lot of lines get merged -- e.g., in row 500 of my data frame, I'll see something like row…
Ruld
19
votes
1 answer

How to read a subset of large dataset in R?

I have a dataset with about 2 million rows, so without reading the whole dataset I want to read a subset of dataset . My dataset contains a date column in it so I just want to read dataset between a date range without reading whole dataset as it…
Zeeshan shaikh
  • 341
  • 1
  • 5
  • 24
19
votes
3 answers

read.csv vs. read.table

I have seen in several cases that while read.table() is not able to read a tab delimited file (for example the annotation table of a microarray) returning the following error: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines,…
Ali
  • 9,440
  • 12
  • 62
  • 92
18
votes
3 answers

read.table reads "T" as TRUE and "F" as FALSE, how to avoid?

I have a file with the data c("A","T","B","F"). When I use: read.csv(myfile,header=F,stringsAsFactors=F) R interprets character T as TRUE and F as FALSE Am I doing anything wrong?
nopeva
  • 1,583
  • 5
  • 22
  • 38
17
votes
3 answers

Numeric variables converted to factors when reading a CSV file

I'm trying to read a .csv file into R where all the column are numeric. However, they get converted to factor everytime I import them. Here's a sample of how my CSV looks like: This is my code: options(StringsAsFactors=F) data<-read.csv("in.csv",…
intael
  • 508
  • 2
  • 7
  • 21
16
votes
2 answers

Preventing column-class inference in fread()

Is there a way for fread to mimic the behaviour of read.table whereby the class of the variable is set by the data that is read in. I have numeric data with a few comments underneath the main data. When i use fread to read in the data, the columns…
user2957945
  • 2,353
  • 2
  • 21
  • 40
15
votes
3 answers

preserve old (pre 3.1.0) type.convert behavior

R 3.1.0 is out and one of the new features is the following: type.convert() (and hence by default read.table()) returns a character vector or factor when representing a numeric input as a double would lose accuracy. Similarly for complex…
flodel
  • 87,577
  • 21
  • 185
  • 223
15
votes
1 answer

Reading a CSV file organized horizontally

In R, is there a function like read.csv that reads in files where the headers are on the left (or right) as opposed to the top and the data is organized from left to right? So the data would look like: var1,1,2,3,4,5 Looking at the documentation…
Jon Claus
  • 2,862
  • 4
  • 22
  • 33
14
votes
1 answer

How to prevent 'read.table' from changing underscores and hyphens to dots?

I have a bunch of files which I'm merging in one data frame. The file names are as such: unc.edu.b6530750-0410-43ec-bb79-f862ca3424a6.1918120.rsem.genes.results And I want the file names to be the column names. I'm using the following code: for…
paul_dg
  • 511
  • 5
  • 16
14
votes
3 answers

How to read data with different separators?

I have a file looks like: a 1,2,3,5 b 4,5,6,7 c 5,6,7,8 ... That the separator between 1st and 2nd is '\t', other separators are comma. How can I read this kind of data set as as dataframe having 5 fields.
yliueagle
  • 1,191
  • 1
  • 7
  • 22
14
votes
3 answers

How to avoid: read.table truncates numeric values beginning with 0

I want to import a table (.txt file) in R with read.table(). One column in my table is an ID with nine numerals - some ids begin with a 0, other with 1 or 2. R truncates the first 0 (012345678 becomes 12345678) which leads to problems when using…
Tim
  • 293
  • 3
  • 9
13
votes
3 answers

automatically detect date columns when reading a file into a data.frame

When reading a file, the read.table function uses type.convert to distinguish between logical, integer, numeric, complex, or factor columns and store them accordingly. I'd like to add dates to the mix, so that columns containing dates can…
flodel
  • 87,577
  • 21
  • 185
  • 223
12
votes
2 answers

Reading in multiple CSVs with different numbers of lines to skip at start of file

I have to read in about 300 individual CSVs. I have managed to automate the process using a loop and structured CSV names. However each CSV has 14-17 lines of rubbish at the start and it varies randomly so hard coding a 'skip' parameter in the…
LoveMeow
  • 1,141
  • 2
  • 15
  • 26
11
votes
1 answer

Why write.csv and read.csv are not consistent?

The problem is simple, consider the following example: m <- head(iris) write.csv(m, file = 'm.csv') m1 <- read.csv('m.csv') The result of this is that m1 is different from the original object m in that it has a new first column named "X". If I…
Juan
  • 1,351
  • 1
  • 14
  • 28
1
2
3
37 38