Reputation: 95
Hello I am trying to import a html table as data.frame and the columns come in as factors. I need to convert them to numeric, which I can do but when I use the single method it would take long and converting them to matrix trims the numbers. Can someone explain how to convert all the numbers in columns 2:6 into numerics that hold up the right numeric length?
nms = c("State/Territory", "FY 2008" ,"FY 2009", "FY 2010", "FY 2011", "FY 2012")
x <- data.frame(readHTMLTable('http://www.fns.usda.gov/pd/15SNAPpartPP.htm'), stringsAsFactors = F)
x <- x[5:57,]
names(x) <- nms
snap.partpp <- x
this is what i have tried to do to solve this problem but it does the conversion but changes the values of the numbers
x <- data.frame(readHTMLTable('http://www.fns.usda.gov/pd/15SNAPpartPP.htm'), stringsAsFactors = F)
y <- x[5:57, 1]
x <- data.matrix(x[5:57,2:6])
xy <- data.frame(y, x)
names(xy) <- nms
snap.avghh <- xy
Upvotes: 0
Views: 113
Reputation: 59355
The answer is in the comments, but is this what you had in mind?
sapply(x[,2:6],function(x){as.numeric(gsub(",","",x))})
Produces this:
FY 2008 FY 2009 FY 2010 FY 2011 FY 2012
[1,] 56977 64385 76445 86044 91298
[2,] 627660 813987 1018171 1067617 1123974
[3,] 377883 411153 466598 486451 502125
[4,] 2220127 2670341 3238548 3672980 3964221
[5,] 252933 319121 404679 453103 491630
[6,] 225383 258165 336064 378677 403466
[7,] 74429 90933 112513 134927 148257
[8,] 89442 103311 118493 134845 141147
[9,] 1454928 1952362 2603185 3074671 3353064
[10,] 1021155 1286078 1591078 1780039 1912839
[11,] 27874 31511 36926 40631 43727 [truncated...]
Upvotes: 1