Prasanna Kumar
Prasanna Kumar

Reputation: 78

Error while trying to read .data file in R

I am trying to read car.data file at this location - https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data using read.table as below. Tried various solutions listed earlier, but did not work. I am using Windows 8, R version 3.2.3. I can save this file as txt file and then read, but not able to read the .data file directly from URL or even after saving using read.table

t <- read.table(
    "https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", 
    fileEncoding="UTF-16", 
    sep = ",", 
    header=F
)

Here is the error I am getting and is resulting in an empty dataframe with single cell with "?" in it:

Warning messages:

1: In read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", : invalid input found on input connection 'https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data'

2: In read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", : incomplete final line found by readTableHeader on 'https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data'

Please help!

Upvotes: 0

Views: 987

Answers (2)

Prasanna Kumar
Prasanna Kumar

Reputation: 78

Thanks to cory, here is the solution - just use read.csv directly:

x <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data")

Upvotes: 0

WillardSolutions
WillardSolutions

Reputation: 2314

Don't use read.table when the data is not stored in a table. Data at that link is clearly presented in comma-separated format. Use the RCurl package instead and read the data as CSV:

library(RCurl)
x <- getURL("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data")
y <- read.csv(text = x)

Now y contains your data.

Upvotes: 3

Related Questions