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
6
votes
1 answer

Error in read.table: !header: invalid argument type

I am having the strangest of issues. The following code no longer works: Test<-matrix(rnorm(9),ncol=3) colnames(Test)<-c("a","b","c") write.table(Test,file="Test.txt") d<-read.table("Test.txt",header=T) I get: Error in !header: invalid…
Pinemangoes
  • 1,158
  • 3
  • 11
  • 13
6
votes
2 answers

Importing "csv" file with multiple-character separator to R?

I have a "csv" text file where each field is separated by \t&%$# which I'm now trying to import into R. The sep= argument of read.table()instists on a single character. Is there a quick way to directly import this file? Some of the data fields are…
Bryan
  • 1,771
  • 4
  • 17
  • 30
6
votes
1 answer

Change the number of rows read.table uses to determine the number of column in R

In ?read.table is stated that: The number of data columns is determined by looking at the first five lines of input (or the whole file if it has less than five lines), or from the length of col.names if it is specified and is longer. This could…
Michele
  • 8,563
  • 6
  • 45
  • 72
6
votes
2 answers

Reading .dat and .dct directly from R

I need to read a .dat file using a .dct file. Has anyone done that using R? The format is: dictionary { # how many lines per record _lines(1) # start defining the first line _line(1) # starting column / storage type / variable name / read…
sdaza
  • 1,032
  • 13
  • 29
6
votes
2 answers

read.table and comments in R

I'd like to add metadata to my spreadsheet as comments, and have R ignore these afterwards. My data are of the form v1,v2,v3, 1,5,7, 4,2,1,#possible error, (which the exception that it is much longer. the first comment actually appears well…
AndrewMacDonald
  • 2,870
  • 1
  • 18
  • 31
5
votes
1 answer

What does the flush function do on read.table in R?

I recently started taking R lectures and I'm currently working on scanning files. On a worksheet, one of my questions is like: Read the file Table6.txt, check out the file first. Notice that the information is repeated, we only want the first…
Levy
  • 55
  • 4
5
votes
1 answer

R: read.table and missing values

When I load my data file in tab delimited format in R, I got this error message: Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 3 did not have 5 elements Here's my data: KEY ID code1 code2 name 1 …
CHONG
  • 373
  • 1
  • 5
  • 13
5
votes
1 answer

Convert long string to data.frame

This is a noob question, but I am getting crazy on this. I have a character vector named bars.list which I download from a FTP Sever. The vector looks like…
Pat
  • 1,299
  • 4
  • 17
  • 40
5
votes
2 answers

fill=TRUE will fail when different number of column occurr after 5 rows in read.table?

Let's say we have a file name test.txt which contains unknown number of columns: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 6 7 8 1 2 3 …
Gahoo
  • 215
  • 2
  • 9
5
votes
1 answer

In R, how to read file with custom end of line (eol)

I have a text file to read in R (and store in a data.frame). The file is organized in several rows and columns. Both "sep" and "eol" are customized. Problem: the custom eol, i.e. "\t&nd" (without quotations), can't be set in read.table(...) (or…
Corrado
  • 625
  • 6
  • 12
5
votes
0 answers

R read.table() Some of the field separator and line breaks not recognized

I ran into this issue while reading this tab delimited table GO_MF.txt into R. Here's part of my input: YAL004W YAL004W Unknown YAL005C SSA1 unfolded protein binding ATPase activity …
Chenxi Qiu
  • 45
  • 3
5
votes
2 answers

R's read.table equivalent in Python

I'm trying to move some of my processing work from R to Python. In R, I use read.table() to read REALLY messy CSV files and it automagically splits the records in the correct format. E.g. 391788,"HP Deskjet 3050 scanner always seems to…
mchangun
  • 9,814
  • 18
  • 71
  • 101
5
votes
1 answer

read.table with comma separated values and also commas inside each element

I'm trying to create a table from a csv file comma separated. I'm aware that not all the rows have the same number of elements so I would write some code to eliminate those rows. The problem is that there are rows that include numbers (in thousands)…
nopeva
  • 1,583
  • 5
  • 22
  • 38
5
votes
1 answer

Multiple na.strings in read.table() function in R

I have a square table and it has two na.strings (e.g. "A" and "B") that I need to turn into NA. So far I can turn either one of those into NA but not both. How should I do this? Can I use a function in that argument? If yes, what function should I…
user2462924
  • 65
  • 1
  • 1
  • 5
4
votes
1 answer

Read.table while using '#' as delimiter does not work?

I have a data file with the # sign as delimiter, that I would like to read with the read.file command. First of all; it's a big data file and I don't want to change the delimiter because: the risk of using a different delimiter that already exists…
FBE
  • 651
  • 2
  • 8
  • 15