将dat文件导入R [英] import dat file into 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="\t")
我想访问data.frame的Value部分,但是,我不确定data.frame的维度,如果我输入ncol(数据),它返回1,我期待三。如何访问此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文件有一些额外的信息。使用跳过
参数跳过它们:
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屋!