本篇博客将介绍如何读取非文本文件。
当我们要读取HTML里面的表格时,我们不能再用read.table()函数了,此时我们需要XML包
> install.packages('XML')
There is a binary version available but the source version is later:
binary source needs_compilation
XML 3.98-1.18 3.98-1.19 TRUE
Binaries will be installed
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/XML_3.98-1.18.zip'
length 4600733 bytes (4.4 MB)
downloaded 4.4 MB
package ‘XML’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\DELL\AppData\Local\Temp\RtmpKedmGT\downloaded_packages
>
> library('XML')
Warning message:
程辑包‘XML’是用R版本3.5.2 来建造的
> #尽管有警示,但是不影响使用。
里面包含一个
readHTMLTable(doc, header = NA,
colClasses = NULL, skip.rows = integer(), trim = TRUE,
elFun = xmlValue, as.data.frame = TRUE, which = integer()……)的函数,可以用于读取网页中的表格数据。
Version:1.0 StartHTML:0000000107 EndHTML:0000007348 StartFragment:0000000127 EndFragment:0000007330
readHTMLTable(doc, header = NA,
colClasses = NULL, skip.rows = integer(), trim = TRUE,
elFun = xmlValue, as.data.frame = TRUE, which = integer(),
...)
参数
doc |
可以是文件名、URL或已解析的HTML内部文档、类XML内部元素节点的HTML节点或包含要解析和处理的HTML内容的字符向量的HTML文档。 |
header |
either a logical value indicating whether the table has column labels, e.g. the first row or a 要么是一个逻辑值,指示表是否有列标签,例如第一行或一个头。要么是一个字符向量,给出结果列的名称。这可以是一个逻辑向量,各个值将依次用于不同的表。这允许调用者控制是否将单个表处理为具有列名。或者,可以通过哪个参数读取特定的表,并控制如何使用单个标量逻辑处理该表。 |
colClasses |
提供表中不同列的数据类型名称的列表或向量,或者用于将字符串值转换为适当类型的函数。NULL值意味着我们应该从结果中删除该列。注意,当前转换发生在矢量转换为数据帧之前(如果as.data.frame为真)。因此,要确保字符向量仍然是字符而不是因子,可以使用stringsAsFactors = FALSE。这通常只适用于单个表,因此也适用于应用于aXML内部元素节点对象的方法。 除了常见的R数据类型的“integer”、“numeric”、“logical”、“character”等名称之外,还可以使用“FormattedInteger”、“FormattedNumber”和“Percent”来指定值的格式,这些值可能是用逗号()分隔数字组的数字,或者是后跟百分号(%)的数字。这种机制允许引入新的类,并在colClasses中将这些类指定为目标。 |
skip.rows |
跳过几行 |
trim |
一个逻辑值,指示是否从内容单元格中删除前导和尾随空格。 |
elFun |
在转换每个单元格时调用的函数(如果指定的话)。目前,只指定了节点。将来,我们可能还会传递列的索引,以便函数具有一些上下文,例如值是行标签还是常规值,或者调用者是否知道列的类型。 |
as.data.frame |
一个逻辑值 ,是否将文件中的矩阵型数据转换为数据框 |
which |
如果表格中有很多表格,利用which来确定选取第几个表格来读,如which=3 就是读取第三个表格的数据 |
... |
currently additional parameters that are passed on to |
但是在读取网页文件的时候,会出现很多错误这与网页的设置有关,怪不着我们。因此我们还是尽量用规则的文件吧。
我们也可以对系统剪贴板文件进行读取,
> read.table('clipboard',sep = ',')
V1
1 8\t5\t2\t1\t2
2 4\t8\t4\t2\t4
3 2\t6\t5\t1\t5
4 5\t5\t4\t2\t7
5 6\t6\t6\t1\t3
6 10\t7\t1\t2\t6
7 6\t9\t6\t1\t5
8 6\t7\t3\t2\t2
9 3\t5\t3\t1\t3
10 7\t6\t2\t1\t8
11 5\t4\t3\t1\t3.158639791
12 5\t2\t2\t1\t2
#剪贴板上的数据默认制表符分割
> read.table('clipboard',sep = '\t')
V1 V2 V3 V4 V5
1 8 5 2 1 2.00000
2 4 8 4 2 4.00000
3 2 6 5 1 5.00000
4 5 5 4 2 7.00000
5 6 6 6 1 3.00000
6 10 7 1 2 6.00000
7 6 9 6 1 5.00000
8 6 7 3 2 2.00000
9 3 5 3 1 3.00000
10 7 6 2 1 8.00000
11 5 4 3 1 3.15864
12 5 2 2 1 2.00000
> #此外还可以用readClipboard来直接读取
readClipboard()
[1] "> read.table('clipboard',sep = ',')" " V1"
[3] "1 8\\t5\\t2\\t1\\t2" "2 4\\t8\\t4\\t2\\t4"
[5] "3 2\\t6\\t5\\t1\\t5" "4 5\\t5\\t4\\t2\\t7"
[7] "5 6\\t6\\t6\\t1\\t3" "6 10\\t7\\t1\\t2\\t6"
[9] "7 6\\t9\\t6\\t1\\t5" "8 6\\t7\\t3\\t2\\t2"
[11] "9 3\\t5\\t3\\t1\\t3" "10 7\\t6\\t2\\t1\\t8"
[13] "11 5\\t4\\t3\\t1\\t3.158639791" "12 5\\t2\\t2\\t1\\t2"
[15] "> read.table('clipboard',sep = '\\t')" " V1 V2 V3 V4 V5"
[17] "1 8 5 2 1 2.00000" "2 4 8 4 2 4.00000"
[19] "3 2 6 5 1 5.00000" "4 5 5 4 2 7.00000"
[21] "5 6 6 6 1 3.00000" "6 10 7 1 2 6.00000"
[23] "7 6 9 6 1 5.00000" "8 6 7 3 2 2.00000"
[25] "9 3 5 3 1 3.00000" "10 7 6 2 1 8.00000"
[27] "11 5 4 3 1 3.15864" "12 5 2 2 1 2.00000"
[29] "> "
>
如果在当前工作目录中有一个在压缩包中的文件,可以直接在扩展名后添加.gz即可。
foreign包内含有能够读取各种文件格式的函数。
当文件格式不规则的时候,我们也可以用readlines()和scan()函数来读取。
readLines(con = stdin(), n = -1L, ok = TRUE, warn = TRUE, encoding = "unknown", skipNul = FALSE)以字符串形式返回
Arguments
con |
a connection object or a character string. |
n |
整数。要读的(最大)行数。 负值表示应该一直读到连接上的输入结束 |
ok |
逻辑值. 是否可以在读取n > 0行之前到达连接的末尾?否则,将生成一个错误。 |
warn |
logical. Warn if a text file is missing a final EOL or if there are embedded nuls in the file. |
encoding |
encoding to be assumed for input strings. It is used to mark character strings as known to be in Latin-1 or UTF-8: it is not used to re-encode the input. To do the latter, specify the encoding as part of the connection |
skipNul |
l逻辑值: 是否跳过空值 |
> setwd('E:/R工作路径')
> readLines('mtcars.csv',n=5)
[1] ",mpg,cyl,disp,hp,drat,wt,qsec,vs,am,gear,carb"
[2] "Mazda RX4,21,6,160,110,3.9,2.62,16.46,0,1,4,4"
[3] "Mazda RX4 Wag,21,6,160,110,3.9,2.875,17.02,0,1,4,4"
[4] "Datsun 710,22.8,4,108,93,3.85,2.32,18.61,1,1,4,1"
[5] "Hornet 4 Drive,21.4,6,258,110,3.08,3.215,19.44,1,0,3,1"