import dat file into R

前端 未结 1 1544
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 03:11

Apologies in advance for the simplicity of this question. I am trying to import a .dat file from a website into R with the following code:

www = \"http://www         


        
相关标签:
1条回答
  • 2020-11-30 03:18

    The dat file has some lines of extra information before the actual data. Skip them with the skip argument:

    read.table("http://www.nilu.no/projects/ccc/onlinedata/ozone/CZ03_2009.dat", 
               header=TRUE, skip=3)
    

    An easy way to check this if you are unfamiliar with the dataset is to first use readLines to check a few lines, as below:

    readLines("http://www.nilu.no/projects/ccc/onlinedata/ozone/CZ03_2009.dat", 
              n=10)
    # [1] "Ozone data from CZ03 2009"   "Local time: GMT + 0"        
    # [3] ""                            "Date        Hour      Value"
    # [5] "01.01.2009 00:00       34.3" "01.01.2009 01:00       31.9"
    # [7] "01.01.2009 02:00       29.9" "01.01.2009 03:00       28.5"
    # [9] "01.01.2009 04:00       32.9" "01.01.2009 05:00       20.5"
    

    Here, we can see that the actual data starts at [4], so we know to skip the first three lines.

    Update

    If you really only wanted the Value column, you could do that by:

    as.vector(
        read.table("http://www.nilu.no/projects/ccc/onlinedata/ozone/CZ03_2009.dat",
                   header=TRUE, skip=3)$Value)
    

    Again, readLines is useful for helping us figure out the actual name of the columns we will be importing.

    But I don't see much advantage to doing that over reading the whole dataset in and extracting later.

    0 讨论(0)
提交回复
热议问题