将 dat 文件导入 R [英] import dat file into R

查看:72
本文介绍了将 dat 文件导入 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为这个问题的简单性提前道歉.我正在尝试使用以下代码将 .dat 文件从网站导入 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 = "http://www.nilu.no/projects/ccc/onlinedata/ozone/CZ03_2009.dat"
data <- read.delim(www, header = TRUE, sep="	") 

我想访问 data.frame 的 Value 部分,但是,我不确定 data.frame 的维度,如果我输入 ncol(data),它会返回 1,而我期望的是 3.如何访问此 data.frame 的第三"列?

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?

推荐答案

dat 文件在实际数据之前有一些额外的信息行.使用 skip 参数跳过它们:

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)

如果您不熟悉数据集,一个简单的检查方法是首先使用readLines检查几行,如下所示:

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"

这里,我们可以看到实际数据从[4]开始,所以我们知道跳过前三行.

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

如果你真的只想要Value列,你可以这样做:

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)

同样,readLines 有助于我们确定将要导入的列的实际名称.

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.

这篇关于将 dat 文件导入 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆