State Name to Abbreviation

State name to abbreviation

1) grep the full name from state.name and use that to index into state.abb:

state.abb[grep("New York", state.name)]
## [1] "NY"

1a) or using which:

state.abb[which(state.name == "New York")]
## [1] "NY"

2) or create a vector of state abbreviations whose names are the full names and index into it using the full name:

setNames(state.abb, state.name)["New York"]
## New York
## "NY"

Unlike (1), this one works even if "New York" is replaced by a vector of full state names, e.g. setNames(state.abb, state.name)[c("New York", "Idaho")]

SQL Select Convert State Name To Abbreviation

With PostgreSQL you can use JSON

select '{"Alabama": "AL", "Alaska": "AK"}'::json->'Alabama'

You can also use a column reference instead of a string literal

select 
'{"Alabama": "AL", "Alaska": "AK"}'::json->example.state
from
(values ('Alabama')) example(state)

Making State abbreviations from State Names

if statename.tolower == "new york" then 
statename = "NY"
else if

so if you are going to go this route I would:

  1. use a switch statement instead of if else switch(state.ToLower()). This will be more efficient than if then statements. The compiler will optimize the switch statement.

  2. If you absolutely must use an if then statement. Do

    string lowerCaseState = state.ToLower().

    if(lowerCaseState == "new york"){....}else if...

This way you are creating a lower case string once (strings are immutable) instead of each part of the if then statement.

In truth, I would probably use a switch statement with a static method.

  1. State names aren't going to change
  2. State abbreviations aren't going to change.

You could create an object to store the values to load them each time the program runs, but why? You might as well let the compiler optimize access for non-changing static values.

Changing abbreviated state names with full name

You can use Series.replace. It can take a dict, where the keys of the dict are values to find, and the values of the dict are the replacements. If a value isn't found in the dict, it will be left as-is.

df['State'] = df['State'].replace(states)

Output:

>>> df
Country State
1 United States Minnesota
2 United States Pennsylvania
3 New Zealand Auckland
4 France Île-de-France
5 United States Florida

How can I convert a state name to it's abbreviation?

Checking for a ZIP code should not be inside the loop, since it's not dependent on the states that you're looping over. Do that before searching for the state abbreviations.

Use the .find() method to find the matching state object, then extract the abbreviation from that.

searchTermHandler(event) {
this.presentLoading();
let searchTerm = event.detail.value;
if (parseInt(searchTerm) != NaN) {
if (searchTerm.length != 5) {
alert("Please enter a 5 digit zip code.");
console.log("A number, but not 5 digits.");
} else {
this.dataService.getFacilitiesByZip(searchTerm);
console.log("A 5 digit number.");
}
} else {
let searchLower = searchTerm.toLowerCase();
let state = this.stateList.find((state) => state.abbreviation.toLowerCase() == searchLower || state.name.toLowerCase() == searchLower);
if (state) {
console.log("State abbreviation is: " + state.abbreviation);
this.dataService.getFacilitiesByState(state.abbreviation);
}
}
}

How can I transform full state names to abbreviations?

If you have two vectors of the same length and want to construct a translation table just add the input vector of state names as the names-attribute of the output vector of state abbreviationa, and then pass in the names as inputs to the "["-function:

 names(state.abb) <- state.name   
# both are in-built values in the `state`-item of default `datasets` package
?state # also See: ?Constants and ?data
dfrm$abbrev <- state.abb[dfrm$states]

Get a state name from abbreviation

You can use a javascript object as a dictionary:

var states = {"NY":"New York", "NJ":"New Jersey"};

and access them like this:

alert(states["NY"]);

so your function that returns the full state name would work like this:

var getStateFullName = function (stateAbbr) {
return states[stateAbbr];
}


Related Topics



Leave a reply



Submit