Description Usage Arguments Details Value Author(s) See Also Examples
View source: R/mergeUShouse.senate.R
Merge a data.frame
regarding some members of the US
Congress with a data.frame
with general information on
all members.
1 2 3 4 | mergeUShouse.senate(x, UScongress=UShouse.senate(),
newrows="amount0",
default=list(member=FALSE, amount=0, #vote="notEligible",
incumbent=TRUE) )
|
x |
a |
UScongress |
a |
newrows |
name of a |
default |
default values for columns of |
1. keyx <- with(x, paste(houseSenate, state, District, sep=":"))
2. keyy <- with(UScongress(houseSenate, state, District, sep=":"))
3. notx <- !is.elememt(keyy, keyx)
4. Y <- UScongress[notx, ]
5. add default columns to Y
6. if(!newrows is not in names(x))x <- cbind(x, newrows=FALSE)
7. Y[, newrows] <- TRUE
8. xY <- rbind(x, Y[c(names(x)])
9. replace 'Democrat' with 'Democratic' in xY[['Party']]
10. Look for NAs in "incumbent" who are nevertheless in UScongress; fix. Thus, if x[['incumbent']] is TRUE or FALSE, this value is not checked in UScongress; it's checked only if NA. The check consists of comparing names for a given Chamber:state:district between strsplit(x[['surname']], ' ')[[1]][1] and strsplit(UScongress[['surname']], ' ')[[1]][1] and similarly for givenName. This allows 'Rogers' in x[['surname']] to match 'Rogers (AL)' in UScongress[['surname']], etc. The algorithm is not perfect, but errors should be rare – and could be fixed manually.
a data.frame
combining x
and UScongress
as desired
Spencer Graves
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | tst <- data.frame(Chamber=factor(rep(c('House', 'Senate'), c(4, 2))),
State=factor(c('Missouri', 'Minnesota', 'Tennessee',
'New York', rep('South Carolina', 2))),
state=factor(c('MO', 'MN', 'TN', 'NY', 'SC', 'SC')),
district=c(4, 1, 8, 18, 2, 3),
surname=c('Hartzler', 'Walz', 'Fincher', 'Maloney',
'Graham', 'DeMint'),
givenName=c('Vicky', 'Timothy J.', 'Stephen Lee',
'Sean Patrick', 'Lindsey', 'Jim'),
party=c('R', 'D', 'R', 'D', 'R', 'D'),
CommitteeMember=rep(c(TRUE, FALSE), c(4, 2)),
amount=c(5000, 2000, 29500, 1000, 1000, 11500),
#xvote=c('Y', 'N', 'Y', 'Y', 'notEligible', 'notEligible'),
incumbent=NA, stringsAsFactors=FALSE )
if(!fda::CRAN()){
tst2 <- mergeUShouse.senate(tst)
# A couple of simple tests; don't test too much,
# because the results of UShouse.senate change,
# and we don't want this test to fail
# due to changes that don't affect Ecdat code
tst3 <- tst2[!tst2$amount0, c(1, 4:6, 8:10)]
row.names(tst) <- row.names(tst3)
## Not run:
all.equal(tst[c(1, 4:6, 8:10)], tst3)
## End(Not run)
# tst3[2] = state = factor with 56 levels,
# and tst[2] only has 5; compare without this
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.