0

I am trying to read wave height data into R using this website

https://www.ndbc.noaa.gov/download_data.php?filename=51201h2017.txt.gz&dir=data/historical/stdmet/

my code is

  buoy <- 51211
  year <- 2017
  
     one_yr <- paste0("https://www.ndbc.noaa.gov/view_text_file.php?filename=", 
                      buoy, "h", year, ".txt.gz&dir=data/historical/stdmet/")
     
     oneBuoy_oneYR.df <- read.csv(one_yr, fill = TRUE)

The resulting output is a data frame that has one column and 8985 observations. I have tried using sep = " " but there are some columns that are separated with two spaces instead of one. I have also tried read.delim

I'm sure there is an easy solution, I just haven't found it.

user8304241
  • 241
  • 1
  • 11
  • 1
    Does this answer your question? [Reading text file with multiple space as delimiter in R](https://stackoverflow.com/questions/16979858/reading-text-file-with-multiple-space-as-delimiter-in-r) – Ricardo Semião e Castro May 31 '21 at 18:21
  • Yes, simply change `sep` accordingly and skip one row: `oneBuoy_oneYR.df <- read.csv(one_yr, sep="", skip=1)` – Parfait May 31 '21 at 18:27

1 Answers1

0

Use fread from data.table. fread will detetec the separator and colClasses automatically for you.

library(data.table)
#> Warning: package 'data.table' was built under R version 4.0.4

buoy <- 51211
year <- 2017

one_yr <- paste0("https://www.ndbc.noaa.gov/view_text_file.php?filename=", 
                 buoy, "h", year, ".txt.gz&dir=data/historical/stdmet/")

oneBuoy_oneYR.df <- fread(one_yr, fill = TRUE)

head(oneBuoy_oneYR.df)
#>     #YY MM DD hh mm WDIR WSPD  GST WVHT   DPD  APD  MWD   PRES  ATMP WTMP  DEWP
#> 1:  #yr mo dy hr mn degT  m/s  m/s    m   sec  sec degT    hPa  degC degC  degC
#> 2: 2017 06 07 14 30  999 99.0 99.0 0.58 15.38 5.66  161 9999.0 999.0 26.4 999.0
#> 3: 2017 06 07 15 00  999 99.0 99.0 0.58 15.38 5.61  156 9999.0 999.0 26.4 999.0
#> 4: 2017 06 07 15 30  999 99.0 99.0 0.55 12.50 5.37  161 9999.0 999.0 26.3 999.0
#> 5: 2017 06 07 16 30  999 99.0 99.0 0.64 12.50 4.97  158 9999.0 999.0 26.3 999.0
#> 6: 2017 06 07 17 00  999 99.0 99.0 0.64 15.38 4.95  158 9999.0 999.0 26.3 999.0
#>     VIS  TIDE
#> 1:   mi    ft
#> 2: 99.0 99.00
#> 3: 99.0 99.00
#> 4: 99.0 99.00
#> 5: 99.0 99.00
#> 6: 99.0 99.00

Created on 2021-05-31 by the reprex package (v0.3.0)

Icaro Bombonato
  • 3,742
  • 1
  • 17
  • 12