[R] Return value associated with a factor

From: GL <pflugg_at_shands.ufl.edu>
Date: Mon, 21 Jun 2010 06:15:23 -0700 (PDT)

I am using the code below to extract census tract information. save.tract$state, save.tract$county and save.tract$tract are returned as factors. In the last three statements, I need to save the actual value of the factor, but, instead, the code is yielding the position of the factor. How do I instead return the value of the factor?

By way of example, for Lon=-82.49574 and Lat=29.71495, the code returns state = 1, county = 1 and tract = 161. The desired results are state=12, county = 001 tract = 002201.

#set libraries


#read input

dbs.in = read.delim("addresses_coded_new.txt", header = TRUE, sep = "\t",

                     quote="\"", dec=".")

#instantiate output

more.columns <- data.frame( state=double(0),

dbs.in <- cbind(dbs.in,more.columns)       

#fiure out how many times to loop

j <- nrow(dbs.in)

#loop through each lab/long and assign census tract

for (i in 1:j) {         


         dbs.in$state[i] <- save.tract$state #this is returning the position
in the list instead of the value
         dbs.in$county[i] <- save.tract$county #this is returning the
position in the list instead of the value
         dbs.in$tract[i] <- save.tract$tract #this is returning the position
in the list instead of the value


View this message in context: http://r.789695.n4.nabble.com/Return-value-associated-with-a-factor-tp2262605p2262605.html
Sent from the R help mailing list archive at Nabble.com.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 21 Jun 2010 - 13:19:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 21 Jun 2010 - 13:40:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive