[R] R help table from HTML

arun smartpink111 at yahoo.com
Wed Feb 26 16:56:20 CET 2014


Hi,
Check the ?str() of my.table.
library(XML)

u='http://www.ininternet.org/calorie.htm'
tables1 = readHTMLTable(u)
my.table1=tables1[[9]] 
with(my.table1,mean(PROTEINE))
#[1] NA
#Warning message:
#In mean.default(PROTEINE) :
#  argument is not numeric or logical: returning NA



 str(my.table1)
'data.frame':    215 obs. of  6 variables:
 $ ALIMENTO   : Factor w/ 215 levels "ACCIUGHE SALATE",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ PROTEINE   : Factor w/ 31 levels "0","1","10","11",..: 18 19 26 11 2 18 19 2 2 17 ...
 $ GRASSI     : Factor w/ 42 levels "0","1","10","100",..: 2 6 1 15 1 2 6 1 1 20 ...
 $ CARBOIDRATI: Factor w/ 39 levels "0","1","10","100",..: 1 1 13 1 37 1 1 3 5 1 ...
 $ CALORIE    : Factor w/ 100 levels "10","100","115",..: 3 25 2 36 47 3 25 76 70 4 ...
 $ COLESTEROLO: Factor w/ 34 levels "0.000","0.006",..: 19 25 1 19 1 17 25 1 1 25 ...
 my.table1[,-1] <- lapply(my.table1[,-1], function(x) as.numeric(as.character(x)))


with(my.table1,mean(PROTEINE))
#[1] 10.81395


#or you could use `colClasses` argument in ?readHTMLTable  

tables = readHTMLTable(u,colClasses=c("character",rep("numeric",5)))

 my.table <- tables[[9]]
with(my.table,mean(PROTEINE))
#[1] 10.81395

Also, it is not recommended to ?attach the dataset.  Use ?with.
Hope it helps.

A.K.


Hello, 

I have imported a html table into R: 

u='http://www.ininternet.org/calorie.htm'
tables = readHTMLTable(u) 
my.table=tables[[9]] 
View(my.table) 
But now I have problems when I want to analyze the data and apply any function, for example 

> mean(PROTEINE) 
Warning message: 
In mean.default(PROTEINE) : 
  argument is not numeric or logical: returning NA 
Please tell me how to import a table so that I could analyze the data properly.



More information about the R-help mailing list