0

Importing .dat file into R console. Hoping to get a clean table so I can convert it back to CSV and manipulate it with other CSV files in Excel.

Below is the entire R console session. The table looks aligned in the console but certainly doesn't when exported to CSV. Try the code and see what happens.

sun <- readLines("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat")
head(sun)
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE, sep=',')
Warning messages:
1: In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string
2: In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  number of items read is not a multiple of the number of columns
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE, sep=' ')
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 1 did not have 87 elements
sun <- read.table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE)
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 9 did not have 15 elements
sun <- read.csv("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", header=TRUE)
sundf <- data.frame(sun)
write.csv(sundf, "sun.csv")

Second try:

 sun <- read_fwf("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat", col_positions = fwf_positions())
Error in stopifnot(length(start) == length(end)) : 
  argument "start" is missing, with no default
Elan
  • 539
  • 1
  • 6
  • 18
  • That data is not delimited is a usable way; it's really in a fixed-width format. Use `read.fwf` rather than `read.table`. See the documentation for more info (or use `read_fwf` from the `readr` package which is more efficient). – MrFlick Apr 26 '16 at 15:32

1 Answers1

0

This worked perfectly:

 sun <- read_table("http://www1.ncdc.noaa.gov/pub/data/ccd-data/pctpos15.dat")
Elan
  • 539
  • 1
  • 6
  • 18