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
149
votes
9 answers

read.csv warning 'EOF within quoted string' prevents complete reading of file

I have a CSV file (24.1 MB) that I cannot fully read into my R session. When I open the file in a spreadsheet program I can see 112,544 rows. When I read it into R with read.csv I only get 56,952 rows and this warning: cit <-…
Ben
  • 41,615
  • 18
  • 132
  • 227
109
votes
4 answers

Specify custom Date format for colClasses argument in read.table/read.csv

Question: Is there a way to specify the Date format when using the colClasses argument in read.table/read.csv? (I realise I can convert after importing, but with many date columns like this, it would be easier to do it in the import…
Tommy O'Dell
  • 7,019
  • 13
  • 56
  • 69
63
votes
12 answers

Issue when importing dataset: `Error in scan(...): line 1 did not have 145 elements`

I'm trying to import my dataset in R using read.table(): Dataset.df <- read.table("C:\\dataset.txt", header=TRUE) But I get the following error message: Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 1 did not…
REnthusiast
  • 1,591
  • 3
  • 16
  • 18
55
votes
5 answers

How can you read a CSV file in R with different number of columns

I have a sparse data set, one whose number of columns vary in length, in a csv format. Here is a sample of the file text. 12223, University 12227, bridge, Sky 12828, Sunset 13801, Ground 14853, Tranceamerica 14854, San Francisco 15595, shibuya,…
CompChemist
  • 903
  • 2
  • 8
  • 15
44
votes
4 answers

Ways to read only select columns from a file into R? (A happy medium between `read.table` and `scan`?)

I have some very big delimited data files and I want to process only certain columns in R without taking the time and memory to create a data.frame for the whole file. The only options I know of are read.table which is very wasteful when I only want…
Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
39
votes
6 answers

How to import a .tsv file

I need to read a table that is a .tsv file in R. test <- read.table(file='drug_info.tsv') # Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : # line 1 did not have 10 elements test <- read.table(file='drug_info.tsv',…
Andrew Voronkov
  • 491
  • 1
  • 4
  • 3
38
votes
5 answers

How do you read multiple .txt files into R?

I'm using R to visualize some data all of which is in .txt format. There are a few hundred files in a directory and I want to load it all into one table, in one shot. Any help? EDIT: Listing the files is not a problem. But I am having trouble going…
Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
33
votes
2 answers

If file exists in folder read it else skip the processing part

I am reading in several *.csv, where the names and paths are determined at runtime. However sometimes there are files with do not exist. For this file I need kind of exception handling. Currently I am reading in my files with: companyFileName…
user2051347
  • 1,609
  • 4
  • 23
  • 34
32
votes
5 answers

How can I read the header but also skip lines - read.table()?

Data.txt: Index;Time; 1;2345; 2;1423; 3;5123; The code: dat <- read.table('data.txt', skip = 1, nrows = 2, header =TRUE, sep =';') The result: X1 X2345 1 2 1423 2 3 5123 I expect the header to be Index and Time, as follows: Index Time 1 …
hans-t
  • 3,093
  • 8
  • 33
  • 39
30
votes
1 answer

How to index an element of a list object in R

I'm doing the following in order to import some txt tables and keep them as list: # set working directory - the folder where all selection tables are stored hypo_selections<-list.files() # change object name according to each…
Mohr
  • 323
  • 1
  • 3
  • 9
29
votes
8 answers

In read.table(): incomplete final line found by readTableHeader

I have a CSV when I try to read.csv() that file, I get the warning warning message: In read.table(file = file, header = header, sep = sep, quote = quote, : incomplete final line found by readTableHeader on ... And I cannot isolate the problem,…
user3351370
  • 305
  • 1
  • 4
  • 10
29
votes
3 answers

Cannot read unicode .csv into R

I have a .csv file, which contains the following data: "Ա","Բ" 1,10 2,20 I cannot read it into R so that the column names are displayed like they are in the file. d <- read.csv("./Data/1.csv", fileEncoding="UTF-8") head(d) Produces the…
Ando Khachatryan
  • 291
  • 1
  • 3
  • 5
29
votes
1 answer

Cannot read file with "#" and space using read.table or read.csv in R

I have a file where the first row is a header. The header can have spaces and the # symbol (there may be other special characters as well). I am trying to read this file using read.csv or read.table but it keeps throwing me errors: undefined columns…
user1631306
  • 4,350
  • 8
  • 39
  • 74
28
votes
6 answers

read.table() and read.csv both Error in Rmd

I want to read a txt in Rmd --- title: "Untitled" output: html_document --- ```{r} country <- read.table("country.txt") country ``` It show error: processing file: Preview-2878539db5c7.Rmd Quitting from lines 6-8 (Preview-2878539db5c7.Rmd) Error…
Liu
  • 403
  • 1
  • 4
  • 7
27
votes
3 answers

Why are Xs added to data frame variable names when using read.csv?

When I use the read.csv() function in R to load data, I often find that an X has been added to variable names. I think I just about always see it it in the first variable, but I could be wrong. At first, I thought R might be doing this because I…
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
1
2 3
37 38