[R] Return value associated with a factor

Joris Meys jorismeys at gmail.com
Mon Jun 21 15:28:04 CEST 2010


Code is not runnable, so can't check why it goes wrong, but tried
already with as.character(save.tract$...) ?
Cheers
Joris

On Mon, Jun 21, 2010 at 3:15 PM, GL <pflugg at shands.ufl.edu> wrote:
>
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list