Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导入html表_Html_R_Web Scraping_Html Table - Fatal编程技术网

导入html表

导入html表,html,r,web-scraping,html-table,Html,R,Web Scraping,Html Table,我已将html表导入到R: require(XML) u='http://www.ininternet.org/calorie.htm' tables = readHTMLTable(u) my.table=tables[[9]] View(my.table) 但是现在我在分析数据和应用任何函数时遇到了问题,例如 > mean(PROTEINE) Warning message: In mean.default(PROTEINE) : argument is not numeric

我已将html表导入到R

require(XML)
u='http://www.ininternet.org/calorie.htm'
tables = readHTMLTable(u)
my.table=tables[[9]]
View(my.table)
但是现在我在分析数据和应用任何函数时遇到了问题,例如

> mean(PROTEINE)
Warning message:
In mean.default(PROTEINE) :
  argument is not numeric or logical: returning NA

请告诉我如何导入表格,以便正确分析数据。

您正在尝试计算“因子”类型变量的平均值:

> lapply(my.table, class)
$ALIMENTO
[1] "factor"

$PROTEINE
[1] "factor"

$GRASSI
[1] "factor"

$CARBOIDRATI
[1] "factor"

$CALORIE
[1] "factor"

$COLESTEROLO
[1] "factor"
您需要先将其转换为数字。考虑:

tmp <- as.numeric(as.character(my.table$PROTEINE))
mean(tmp)
## [1] 10.81395

tmp它们都是因素,将它们更改为字符和数字,如下所示:

my.table[,1] <- sapply(my.table[,1], as.character)
my.table[,2:6] <- sapply(my.table[,2:6], function(x) as.numeric(as.character(x))

使用
str(my.table)
查看您的对象,它将快速轻松地向您显示表的重要细节。
tables = readHTMLTable(u,stringsAsFactors=F)
my.table[,2:6] <- sapply(my.table[,2:6], as.numeric)