Convert data types from string to list (Coastal Buoy data) for DateListPlot

2018-03-13 17:01:50

I downloaded Coastal buoy data from the CDIP webpages and saved it as a text file. The trouble is that the data has spaces between the YYY MM DD hh mm.

With FileNames and Import, I was able to import the data from a text file.

fn = FileNames["*.txt"]; (*I am interested in the first text file in this list*)

h2018 = Import[fn[[1]], "Data"]

What I want to do is convert the first 5 columns to DateList and then plot the 9th column against this new DateList.

I tried doing this to no effect because I am trying to convert the data to a string and then replacing spaces in the new string with / and then using DateList.

StringReplace[ToString[h2018[[3 ;;, 1 ;; 3]]], "," .. -> "/"];

StringReplace[%, " " -> ""];


How do I convert this string to a DateList and complete my plotting of column 9 in h2018 with the DateList, dl?

I did try to convert this string to a list of numbers using the information in this question but without success.


  • Here's one way to do it:

    data = Import["",

    "Table", HeaderLines -> 2][[All, {1, 2, 3, 4, 5, 9}]];

    ts = TimeSeries[Composition[Flatten, MapAt[DateObject, #, 1] &,

    Reverse, TakeDrop[#, -1] &] /@ data];


    (If you don't want to convert to a TimeSeries[], you can omit that part.)

    2018-03-13 19:22:10