| mergeVote {Ecfun} | R Documentation |
Merge Roll Call Vote
Description
Merge roll call vote record with a
data.frame containing other
information. The vote records are
typically incomplete, so match first on
houseSenate and surname.
If this match is incomplete, try using
givenName. If that fails, try
state and district, which may
not always be present in vote.
Usage
mergeVote(x, vote, Office="House", vote.x,
check.x=TRUE)
Arguments
x |
a |
vote |
a |
Office |
Either "House" or "Senate"; ignored if
|
vote.x |
name of a column of |
check.x |
logical: If TRUE, check for rows of
|
Details
1. Parse vote.x to get the name of
the column of x into which to write
the vote column of the vote
data.frame.
2. If the vote data.frame
contains a column Office, ignore the
Office argument. Otherwise, add the
argument houseSenate as a column of
vote.
3. Create keyx <- with(x, paste(Office,
surname, sep=":")),
keyx2 <- paste(keyx, givenName, sep=":"),
keyx. <-
paste(houseSenate, state, district, sep=":"),
and similarly keyv, leyv2, and
keyv. from vote.
4. Look for keyv in keyx. When a
unique match is found, transfer the vote the
vote column of x. When no
match is found, try for keyv2 in
keyx2 or keyv. in keyx.
If those fail, print an error message with the
information from vote on all failures
and ask the user to add state and
district information.
5. if(check.x), check for rows in
x[, vote.x] that are NOT
notEligible but are also not in
vote: Throw an error if any are found.
Value
a data.frame with the same
columns as x with its vote column
modified per the vote argument.
Author(s)
Spencer Graves
See Also
Examples
##
## 1. Test good cases
##
votetst <- data.frame(
surName=c('Smith', 'Jones', 'Graves', 'Jsn', 'Jsn', 'Gay'),
givenName=c("Sam", "", "", "John", "John", ''),
votex=factor(c('Y', 'N', 'abstain', 'Y', 'Y', 'Y')),
State=factor(rep(c("CA", "", "SC", "NY"), c(1, 2, 1, 2))),
district=rep(c("13", "1", "2", "1"), c(1, 2, 2, 1)),
stringsAsFactors=FALSE )
x1 <- data.frame(
Office=factor(rep(c("House", "Senate"), e=8)),
state=rep(c("NY", "SC", "SD", "CA", "AK", "AR", "NY", "NJ"), 2),
District=rep(c("2", "2", "At Large", "13", "1", "9", "1", "3"), 2),
surname=rep(c('Jsn', 'Jsn', 'Smith', 'Smith', 'Jones',
'Graves', 'Rx', 'Agnew'), 2),
givenName=rep(c("John D.", "John J.",
"Samual", "Samual", "Mary", "Mary", "Susan", 'Spiro'), 2),
don=1:16, stringsAsFactors=FALSE)
x1. <- mergeVote(x1, votetst)
x2 <- cbind(x1, votex=factor( rep(
c('Y', 'notEligible', 'Y', 'N', 'abstain', 'Y', 'notEligible'),
c(2,1,1,1,1,1,9) ) ) )
all.equal(x1., x2)
##
## 2. Test a case with a vote error in x
##
x1a <- cbind(x1, voterr=rep(
c('notEligible', 'Y', 'notEligible'), c(7, 1, 8)))
x1a. <- try(mergeVote(x1a, votetst))
class(x1a.)=='try-error'