Reputation: 65
I am trying to import a data set from a .dat
file off the internet using the read.table
command. I usually have no issues when the file is formatted, for example:
A B
1 2
3 4
But this data set is formatted
A B A B
1 2 3 4
5 6 7 8
(You can find the data set I'm having issues with here: https://www2.isye.gatech.edu/~jeffwu/book/data/BrainandBodyWeight.dat)
My current line of code is:
Data2 = read.table("https://www2.isye.gatech.edu/~jeffwu/book/data/BrainandBodyWeight.dat", header = TRUE)
The error I'm getting is:
Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 12 elements
Upvotes: 0
Views: 299
Reputation: 24878
The problem is there are spaces in the header row, so just skip that with skip = 1
.
From there, we can extract the even and odd rows using a repeating logical vector c(TRUE, FALSE)
and c(FALSE, TRUE)
.
The final line of the data has some empty values, so remove those with complete.cases()
.
data <- read.table("https://www2.isye.gatech.edu/~jeffwu/book/data/BrainandBodyWeight.dat",
header = FALSE, fill = TRUE, skip = 1)
result <- data.frame(Body.Wt = unname(unlist(data[,c(T,F)])),
Brain.Wt = unname(unlist(data[,c(F,T)])))
result <- result[complete.cases(result),]
head(result)
Body.Wt Brain.Wt
1 3.385 44.5
2 0.480 15.5
3 1.350 8.1
4 465.000 423.0
5 36.330 119.5
6 27.660 115.0
Upvotes: 1