I'm doing a point in polygon analysis to replace lat/lons that = 0 with county centroid lat/lons from a shape file. Forgive me if this question seems trivial, but I'm new to R and my solutions haven't seemed to work out nor have I found very similar questions on here.
Here's an example of what Table A looks like:
ID | Lat | Lon | State_FIPS | County_FIPS
------------------------------------------
1 | 33 | -88 | 028 | 087
2 | 31 | -98 | 048 | 225
3 | 0 | 0 | 017 | 034
4 | 39 | -100| 020 | 063
Then I have the county shape file, which includes stateFIPS, countyFIPS, and lat/lons. Here's what I have so far:
#get shape file
zfn <- "tl_2010_us_county10_simplified"
cns.shp <- readOG(".", zfn)
#create spdf from table A
a.df <- a.spdf
#make lat/lon into coordinates
coordinates(a.spdf) = ~LON+LAT
#set to same projection as shape file
proj4string(a.spdf) <- proj4string(cns.shp)
#point in polygon analysis
events.in <- over(as(a.spdf, "SpatialPoints"), as(cns.shp, "SpatialPolygons"))
a.spdf$LAT = cns.shp$INTPTLAT10[events.in]
a.spdf$LON = cns.shp$INTPTLON10[events.in]
a.df <- as.data.frame(a.spdf)
INTPTLAT10 and INTPTLON10 are the lat/lon centroids from the shape file.
There's a couple problems here:
1) The following code generates an error because LAT/LON are coordinates.
a.spdf$LAT = cns.shp$INTPTLAT10[events.in]
a.spdf$LON = cns.shp$INTPTLON10[events.in]
The error I'm receiving is below:
Error in `$<-`(`*tmp*`, "LAT", value = c(657L, 1787L, 338L, 859L, 901L, :
LAT is a coordinate name, please choose another name.
Is it possible to replace these values? Do I have to change them back from coordinates?
2) I'm not sure how to specify that I only want to replace the LAT/LON values that are equal to 0.
Would this be an ifelse statement? Assuming I can somehow fix the coordinate error, would it look something like this?
a[a.spdf$LAT == 0] <- cns.shp$INTPTLAT10[events.in]
a[a.spdf$LON == 0] <- cns.shp$INTPTLON10[events.in]
I'm sure there are plenty of errors in my code, again R is very new to me. Thank you, any help is greatly appreciated, I have been stuck on this all day!