Select to view content in your preferred language

How to create a new variable in GIS

4708
14
03-21-2017 06:16 PM
ABDALLAMOHAMED
Frequent Contributor

Hello everybody,

I wanted to create a new variable out of existing ones, the same idea as creating dummy variable. I have my data in shapefile as follows:

metro_cities_names, metro_pop,  typology_type. I wanted to recreate new variables from them as follows:

from Metro_pop i want to create a variable called metro_size less than 500, 500-1000000, 1000000-2000000 etc

From typology_type  I want to create a variable called typogy_type which will have four level: 1 = Black in low poverty, 2= Black in high poverty, 3= Latinos in low poverty, 4= Latinos in high poverty.

From metro-cities_names i want to create a variable called Regional_type where I will be able to categorize these 100 metro cities into  regions e.g Northeast, Midwest, south and west.

I know how to do them in SPSS but I hate it, plus It would be easy for me to run my OLS in Arcmap than SPSS.

Thanks a lot for your input

0 Kudos
14 Replies
ABDALLAMOHAMED
Frequent Contributor

Hi Dan,

This sounds great, very straightforward. I'm going to give it a try for sure. My question is can I use the same code for other variables too?

Thanks a lot

appreciated!

0 Kudos
ABDALLAMOHAMED
Frequent Contributor

Dan,

I have one numerical variable which is population size and I wanted to categorize it into  e.g. 

!- less than 500,000

2- 500000-1000000

3- 1 million to 2 million 

4- etc .....

can I use the same exact code?

0 Kudos
ABDALLAMOHAMED
Frequent Contributor

Hi Rebecca.. Not quite sure but It looks like subtypes may work. I will give a try. My concern is that it might not work with OLS Ordinary Least Squares (OLS)—Help | ArcGIS Desktop 

0 Kudos
DanPatterson_Retired
MVP Emeritus

reclassification can be done one at a time by querying for a condition, then using the field calculator on a new field and providing a value... ie, query for values < 500000 in one field, then in your new field you can use the field calculator to assign a value of ... say... 1 to that class.  Repeat for subquent classes.  In short, when records are selected in a table, and a field calculation is then performed, only the selected records for the appropriate field are calculated.  Repeating your condition 3 or 4 times is probably the quickest since I suspect that your comfort level with scripts isn't quite what you want it to be.

But, if you want to read up on field calculator expressions using python code blocks, when you have some leisure time, then this might work. The whole thing gets copied into the code block and the last line (without the # comment, eg. np_reclass(!fieldname!) ) gets copied in the expression box.  Obviously !fieldname! is the field name containing the old data and line 3 represents the new 'breaks'/'bins' to reclassify your data into..   BUT like I said, do it manually in your case, but keep this in mind should you have to do sequential numerical reclassifications in the future.

import numpy as np
# ----  change the values below in sequential order ----
bins = [500000, 1000000, 2000000, 5000000]  
#
def np_reclass(val, bins, inc_by=1):
    """useage: np_reclass(!fieldname!)"""
    cls = (np.digitize( val, bins, right=True)) + inc_by
    return cls
# ---- useage ---- copy this whole thing to the code block
# np_reclass(!fieldname!)  # this goes in the expression box
ABDALLAMOHAMED
Frequent Contributor

Okay, that is great.

Thanks a lot

Best!

0 Kudos