[R] convert factor to numeric in bulk
arun
smartpink111 at yahoo.com
Thu Dec 5 23:11:11 CET 2013
Hi,
colnames(x1) <- gsub("NULL\\.","",colnames(x1)) ##not needed.
A.K.
On Thursday, December 5, 2013 5:04 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
Try:
library(XML)
x1 <- readHTMLTable('http://www.fns.usda.gov/pd/15SNAPpartPP.htm',skip.rows=c(1:2,4,58:60),colClasses=c("character",rep("FormattedNumber",5)),stringsAsFactors=FALSE)[[1]]
colnames(x1) <- gsub("NULL\\.","",colnames(x1))
str(x1)
#'data.frame': 53 obs. of 6 variables:
# $ State/Territory: chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
# $ FY 2008 : num 571591 56977 627660 377883 2220127 ...
# $ FY 2009 : num 679138 64385 813987 411153 2670341 ...
# $ FY 2010 : num 805095 76445 1018171 466598 3238548 ...
# $ FY 2011 : num 920365 86044 1067617 486451 3672980 ...
# $ FY 2012 : num 910244 91298 1123974 502125 3964221 ...
A.K.
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
More information about the R-help
mailing list