导入html表
我已将html表导入到R:导入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
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)