Learn R Programming

pvsR (version 0.1.1)

District.getByOfficeState: Get district IDs according to the office and state

Description

This function is a wrapper for the District.getByOfficeState() method of the PVS API District class which grabs district IDs according to the office and state. The function sends a request with this method to the PVS API for all district names and office IDs given as a function input, extracts the XML values from the returned XML file(s) and returns them arranged in one data frame.

Usage

District.getByOfficeState(officeId, stateId,
    districtName=NULL, all=FALSE)

Arguments

officeId
a character string or list of character strings with the office ID(s) (see references for details)
stateId
a character string or list of character strings with the state ID(s) (see references for details)
districtName
(optional) a character string or list of character strings with the district name (default: All)
all
a logical indicator; if TRUE data on all possible combinations of the input variables are returned, if FALSE (default) only the exact combinations of them (see example)

Value

  • A data frame with a row for each district and columns with variables describing the district. The returned data frame contains a row for each district and columns with the following variables describing the district: districtList.district*.districtId, districtList.district*.name, districtList.district*.officeId, districtList.district*.stateId.

References

http://api.votesmart.org/docs/District.html See http://api.votesmart.org/docs/semi-static.html for a list of office IDs or use Office.getOfficesByType(), Office.getOfficesByLevel(), Office.getOfficesByTypeLevel() or Office.getOfficesByBranchLevel(). Use State.getStateIDs() to get a list of state IDs.

Examples

Run this code
# First, make sure your personal PVS API key is saved as character string in the pvs.key variable:
pvs.key <- "yourkey"
# get districts for certain office and state IDs
districts <- District.getByOfficeState(officeId=list(8,9),stateId=list("NY","NJ"))
district
# get a data frame of districts according to all state/office/districtName combinations
districts <- District.getByOfficeState(officeId=list(8,9),stateId=list("NY","NJ"),
 districtName=list(1,2), all=TRUE)
districts
# get a data frame of districts according to the exact state/office/districtName combinations
# (i.e., 8/"NY"/1, 9/"NJ"/2)
districts <- District.getByOfficeState(officeId=list(8,9),stateId=list("NY","NJ"),
 districtName=list(1,2), all=FALSE)
districts

Run the code above in your browser using DataLab