我以HTML表格的形式从数据库转储。 我的问题是它使用逗号作为十进制字符,我无法获得readHTMLTable来正确处理它。值最终为因子而不是数字。这可以在外部解决,但我想在R中完成所有工作。
我试图传递dec=","
,希望elipsis将它传递给执行管道,但它不起作用。
下一个trest的灵感来自于我尝试使用elFun
library(XML)
tryAsNumeric <- function(node) {
val = xmlValue(node)
ans = as.numeric(gsub(",", ".", val))
if(is.numeric(ans))
ans
else
val
}
tmp_list <- readHTMLTable("teeChart.xls", elFun = tryAsNumeric)
结束了这条消息
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In (function (node) ... : NAs introduced by coercion
2: In (function (node) ... : NAs introduced by coercion
3: In (function (node) ... : NAs introduced by coercion
4: In (function (node) ... : NAs introduced by coercion
简洁截断列表。
这是一个用于再现性的简化表。 (teeChart.xls)
<table border="1">
<tr><td></td><td>Lägenhet 053</td><td></td><td>Lägenhet 054</td><td></td><td>Lägenhet 055</td><td></td></tr>
<tr><td>Index</td><td>X</td><td>Y</td><td>X</td><td>Y</td><td>X</td><td>Y</td></tr>
<tr><td>0</td><td>42309</td><td>20,8249988555908</td><td>42309</td><td>20,2000007629395</td><td>42309</td><td>22,2000007629395</td></tr>
<tr><td>1</td><td>42309,0416666667</td><td>20,7000007629395</td><td>42309,0416666667</td><td>20,2000007629395</td><td>42309,0416666667</td><td>22,125</td></tr>
<tr><td>2</td><td>42309,0833333333</td><td>20,6000003814697</td><td>42309,0833333333</td><td>20,2000007629395</td><td>42309,0833333333</td><td>22,0249996185303</td></tr>
</table>
答案 0 :(得分:0)
设置colClasses
?也来自帮助?readHTMLTable
:
library(XML)
tryAsNumeric <- function(node) {
val = xmlValue(node)
ans = as.numeric(gsub(",", ".", val))
if(all(is.numeric(ans)))
ans
else
val
}
txt <- readLines(n=7)
<table border="1">
<tr><td></td><td>Lägenhet 053</td><td></td><td>Lägenhet 054</td><td></td><td>Lägenhet 055</td><td></td></tr>
<tr><td>Index</td><td>X</td><td>Y</td><td>X</td><td>Y</td><td>X</td><td>Y</td></tr>
<tr><td>0</td><td>42309</td><td>20,8249988555908</td><td>42309</td><td>20,2000007629395</td><td>42309</td><td>22,2000007629395</td></tr>
<tr><td>1</td><td>42309,0416666667</td><td>20,7000007629395</td><td>42309,0416666667</td><td>20,2000007629395</td><td>42309,0416666667</td><td>22,125</td></tr>
<tr><td>2</td><td>42309,0833333333</td><td>20,6000003814697</td><td>42309,0833333333</td><td>20,2000007629395</td><td>42309,0833333333</td><td>22,0249996185303</td></tr>
</table>
doc <- htmlParse(txt, asText=TRUE)
( res <- readHTMLTable(doc, elFun = tryAsNumeric, colClasses = rep("numeric", 7)) )
# $`NULL`
# NA NA NA NA NA NA NA
# 1 NA NA NA NA NA NA NA
# 2 0 42309.00 20.825 42309.00 20.2 42309.00 22.200
# 3 1 42309.04 20.700 42309.04 20.2 42309.04 22.125
# 4 2 42309.08 20.600 42309.08 20.2 42309.08 22.025
str(res)
# List of 1
# $ NULL:'data.frame': 4 obs. of 7 variables:
# ..$ NA: num [1:4] NA 0 1 2
# ..$ NA: num [1:4] NA 42309 42309 42309
# ..$ NA: num [1:4] NA 20.8 20.7 20.6
# ..$ NA: num [1:4] NA 42309 42309 42309
# ..$ NA: num [1:4] NA 20.2 20.2 20.2
# ..$ NA: num [1:4] NA 42309 42309 42309
# ..$ NA: num [1:4] NA 22.2 22.1 22
library(XML)
txt <- readLines(n=7)
<table border="1">
<tr><td></td><td>Lägenhet 053</td><td></td><td>Lägenhet 054</td><td></td><td>Lägenhet 055</td><td></td></tr>
<tr><td>Index</td><td>X</td><td>Y</td><td>X</td><td>Y</td><td>X</td><td>Y</td></tr>
<tr><td>0</td><td>42309</td><td>20,8249988555908</td><td>42309</td><td>20,2000007629395</td><td>42309</td><td>22,2000007629395</td></tr>
<tr><td>1</td><td>42309,0416666667</td><td>20,7000007629395</td><td>42309,0416666667</td><td>20,2000007629395</td><td>42309,0416666667</td><td>22,125</td></tr>
<tr><td>2</td><td>42309,0833333333</td><td>20,6000003814697</td><td>42309,0833333333</td><td>20,2000007629395</td><td>42309,0833333333</td><td>22,0249996185303</td></tr>
</table>
doc <- htmlParse(txt)
m <- as.matrix(readHTMLTable(doc, which=1))
colnames(m) <- m[1,]
m <- m[-1, ]
m <- gsub(",", ".", m)
as.data.frame(structure(as.numeric(m), .Dim=dim(m), .Dimnames = dimnames(m)))
# Index X Y X Y X Y
# 1 0 42309.00 20.825 42309.00 20.2 42309.00 22.200
# 2 1 42309.04 20.700 42309.04 20.2 42309.04 22.125
# 3 2 42309.08 20.600 42309.08 20.2 42309.08 22.025