Help merging two datasets using geo info

11 views
Skip to first unread message

Linda Estelí Méndez

unread,
Apr 12, 2017, 4:59:37 PM4/12/17
to Davis R Users' Group
Hi all,

I apologize if this is a stupid question. I've been asking around, tried to use stackoverflow etc, and haven't been able to make this work. Hope someone here can help me!

What I want to do:
I have two datasets - "agency" and "casgem". "casgem" is a shapefile with information of groundwater basins in California. "agency" is a dataset I've been trying to compile with a couple columns that have shapefile information and also another column "BasinNo" with is an ID for the groundwater basin in which agencies are located. My goal is to somehow match the datasets in a way that I know where is each agency located, so basically have information in the "BasinNo" for each agency.

Thanks to anyone has any idea. I've tried to use over (sp package) but didn't really get anywhere...

Warm regards,

Linda.



agency.csv
CASGEM_Groundwater_Basin_Prioritization.zip

Ryan Peek

unread,
Apr 12, 2017, 8:10:21 PM4/12/17
to davi...@googlegroups.com
Hi Linda,

I think what you are requesting is along these lines...but I'm not sure. Take a look.

library(rgdal)

# read in shape
gw <- readOGR(dsn = "data/shp", layer="CASGEM_Groundwater_Basin_Prioritization")

# quick plot
plot(gw)

# read in Agency data
agency<- read.csv("data/shp/agency.csv")

# make a new copy of Spatial Polygons
gw2 <- gw

# view summary of data
summary(agency)
summary(gw2@data)

# join on Basin, make sort F to retain spatial order
gw2@data <- merge(gw2@data, agency, by.x="BAS_SBBSN", by.y="BasinNo", sort=F)

# plot
plot(gw2, col=gw2$OWNERSHIP)

Inline image 1

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--

 
"When we try to pick out anything by itself, we find it hitched to everything else in the universe."
John Muir (My First Summer in the Sierra, 1911)
-----------------------------------------------------
Ryan Peek
PhD Candidate, Graduate Group in Ecology
Center for Watershed Sciences, UC Davis

-----------------------------------------------------

Linda Estelí Méndez

unread,
Apr 13, 2017, 4:38:40 PM4/13/17
to davi...@googlegroups.com
Hi Ryan,
Thanks for answering! 

I checked what you did and yes this is what I want to do, but the outcome you got has only 189 agencies. The 'agency' dataset has 1239 agencies. The majority of those agencies have geographic information in a column called Shape_STAr and Shape_STLe and 189 of those agencies have information in the "BasinNo" that I inputed by hand. So basically with the code, R recognized those hand-written agencies that already have a BasinNo ID, but what I am trying to avoid is to handwrite that for over a thousand agencies. So what I really want to do is use that Shapefile information in those columns and see if we can do a merge with that, in the absence of having a BasinNo for the agencies. I don't even know if its possible, but worth asking.

Thanks so much for your help!!

Linda.


You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/Nh3_itgO5Lk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+unsubscribe@googlegroups.com.

Ryan Peek

unread,
Apr 13, 2017, 6:24:03 PM4/13/17
to davi...@googlegroups.com
Hi Linda, 

I think you'd need to use the same approach, but unless you have some sort of cross-walk column (something with joinable information in both), you may have trouble getting this to work as it currently exists. I'm not sure what Shape_STAr field you could join with in the shp file...is that a location (UTM or coordinate field)?

Any chance there's another (third) table of data with metadata or spatial data that you could use to join to the two you sent? If so, you could use the (third) data table that has some replicable field with both your shp and the agencies, and then join (or crosswalk) to one or both of the tables as you need. 

Another option would be getting the agency jurisdiction info from another shapefile and joining by agency (check out CPAD or CA land ownership...they have pretty detailed info you might be able to use). You could also try using something like you mentioned earlier, the "over()" function if you have two spatial polygon/point files to extract that info. 

Not sure what else I can do to help...come by the D-RUG meeting on Tuesday at 10am and we can take a look. 

Adios,
Ryan

Erica Rettig

unread,
Apr 13, 2017, 6:47:24 PM4/13/17
to davi...@googlegroups.com
What are Shape_STAr and Shape_STLe, exactly? It's solvable if you have more information about the geography

Thanks,
Erica


Erica Rettig
PhD Candidate
Graduate Group in Ecology, UC Davis
Reply all
Reply to author
Forward
0 new messages