[R] Return value associated with a factor

GL pflugg at shands.ufl.edu
Mon Jun 21 15:15:23 CEST 2010


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   
library(UScensus2000tract)
library(gdata)
data(florida.tract)


#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), 
                            county=double(0), 
                            tract=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) {  
        
index<-overlay(SpatialPoints(cbind(dbs.in$Lon[i],dbs.in$Lat[i])),florida.tract)
         save.tract<-florida.tract[index,] 
         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.



More information about the R-help mailing list