import dat file into R

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 = ""
data <- read.delim(www, header = TRUE, sep="\t") 

I want to access the Value portion of the data.frame, however, I am unsure about the dimensions of the data.frame, if I type ncol(data) it returns 1 which I was expecting three. How do I access the "third" column of this data.frame?

0

1 Answer

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

read.table("", 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("", 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("", 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.

6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like