Zar
Zar

Reputation: 47

Reading text file with read.fwf

I get an error when I am trying to read a text file in R:

"Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : line 2 did not have 5 elements"

I tried to read a text file with the following code:

read.fwf(file=url("http://d396qusza40orc.cloudfront.net/getdata%2Fwksst8110.for"),
         widths=c(9,8, 8, 8, 8),sep = "", skip = 2, header= TRUE)

I loaded packages readr.

Upvotes: 0

Views: 562

Answers (1)

phiver
phiver

Reputation: 23608

You wrote that you are using package readr, but you use the base function to read a fixed width file.

With readr loaded you can use the following to load the data. And as Pascal mentioned skip the first 3 lines, 4 if you specify col_names = FALSE (see code). Also there is no need to specify the fixed width as it is a nice table format split by white spaces. The function read_table should work fine.

library(readr)

file <- "http://d396qusza40orc.cloudfront.net/getdata%2Fwksst8110.for"
df2 <- read_table(file, skip = 4, col_names = FALSE)

# Check information in file and check how the headers should be
read_lines(file, n_max = 4)

[1] " Weekly SST data starts week centered on 3Jan1990"              ""                                                              
[3] "                Nino1+2      Nino3        Nino34        Nino4"  " Week          SST SSTA     SST SSTA     SST SSTA     SST SSTA"

# Set correct header names. Manually was faster than programming.
names(df2) <- c("Week", "Nino1+2 SST SSTA", "Nino3 SST SSTA", "Nino34 SST SSTA", "Nino4 SST SSTA") 
head(df2)

       Week Nino1+2 SST SSTA Nino3 SST SSTA Nino34 SST SSTA Nino4 SST SSTA
1 03JAN1990         23.4-0.4       25.1-0.3        26.6 0.0       28.6 0.3
2 10JAN1990         23.4-0.8       25.2-0.3        26.6 0.1       28.6 0.3
3 17JAN1990         24.2-0.3       25.3-0.3        26.5-0.1       28.6 0.3
4 24JAN1990         24.4-0.5       25.5-0.4        26.5-0.1       28.4 0.2
5 31JAN1990         25.1-0.2       25.8-0.2        26.7 0.1       28.4 0.2
6 07FEB1990         25.8 0.2       26.1-0.1        26.8 0.1       28.4 0.3 

Upvotes: 0

Related Questions