Reputation: 1271
I am trying to read a .txt file from the internet and get it into a useable form in R. Seems like it should be easy, but I am struggling:
Data is from Berkeley Earth:
b_earth_url <- 'http://berkeleyearth.lbl.gov/auto/Global/Land_and_Ocean_complete.txt'
I have tried the following:
read.table(b_earth_url, sep = '\t', comment.char = '%', row.names = NULL)
or:
b_earth_data <- readLines(b_earth_url)[!grepl('%', readLines(b_earth_url))]
data.frame(b_earth_data, stringsAsFactors = F)
I have tried a few other options, but can't get past a dataframe with a single variable, containing a fixed width chr vector.
I have tried extract()
, separate()
and strsplit()
, and can't get any of them to work. I don't think I know how to use a fixed width separator for sep =
Upvotes: 1
Views: 41
Reputation: 11076
The separator is whitespace (blanks) not tabs:
out <- read.table(b_earth_url, comment.char = '%')
head(out)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
# 1 1850 1 -0.781 0.382 NaN NaN NaN NaN NaN NaN NaN NaN
# 2 1850 2 -0.260 0.432 NaN NaN NaN NaN NaN NaN NaN NaN
# 3 1850 3 -0.399 0.348 NaN NaN NaN NaN NaN NaN NaN NaN
# 4 1850 4 -0.696 0.296 NaN NaN NaN NaN NaN NaN NaN NaN
# 5 1850 5 -0.690 0.320 NaN NaN NaN NaN NaN NaN NaN NaN
# 6 1850 6 -0.392 0.228 -0.529 0.147 NaN NaN NaN NaN NaN NaN
Upvotes: 3