Sciolism Apparently
Sciolism Apparently

Reputation: 355

Reading two-column data with read.table results in three columns

I am using read.table() to get data from a web page. The data table has two columns and is from NIST. Following is my code, including the URL if you want to preview the data:

    options(digits = 12)
    theURL <- "https://www.itl.nist.gov/div898/strd/anova/AtmWtAg.dat"
    AgData <- read.table(theURL, header = TRUE , skip = 59)
    # Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
    # line 1 did not have 3 elements
    AgData$Instrument = as.factor(AgData$Instrument)
    fitAgData = aov(AgWt ~ Instrument, data=AgData)

I have inserted the error message as a comment in the code where it occurs.

Other answers on stack exchange seem to deal with missing values causing this error. The data on this site is complete, so I'm not sure what is causing the error.

So far, I have fiddled with the skip = value; inserted the column names as a read.table argument; and added fill = TRUE to read.table The latter one resulted in a data table with three columns, one containing NA values. Since there are names in the table, I have the header=TRUE argument on.

Somehow read.table() thinks there are three columns, and I don't see a way to tell it there are two.

After the skipped rows, I saw the start of the file like this:

Data:  
      Instrument           AgWt
          1            107.8681568
          1            107.8681465
          1            107.8681572
          1            107.8681785

In fact, the data looks like this:

Data:  Instrument           AgWt
           1            107.8681568
           1            107.8681465
           1            107.8681572
           1            107.8681785

Upvotes: 0

Views: 573

Answers (1)

user10917479
user10917479

Reputation:

It identifies "Data:" as the name of your first column. So it scans the header and identifies 3 columns, then gets to the data and only sees 2. Try skipping that line and then manually naming your data.

Upvotes: 1

Related Questions