Expand a dataset on some members of the US Congress to the entire membership

Description

Merge a data.frame regarding some members of the US Congress with a data.frame with general information on all members.

Usage

1
2
3
4
mergeUShouse.senate(x, UScongress=UShouse.senate(),
        newrows="amount0",
        default=list(member=FALSE, amount=0, vote="notEligible",
                     incumbent=TRUE) )

Arguments

x

a data.frame to be merged with UScongress

UScongress

a data.frame to be merged with x.

newrows

name of a logical column to add that is TRUE for rows added to x and FALSE otherwise.

default

default values for columns of x identified by regexpr(names(default)[i], tolower(names(x))).

Details

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 Office: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.

Value

a data.frame combining x and UScongress as desired

Author(s)

Spencer Graves

See Also

merge UShouse.senate

Examples

 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
35
tst <- data.frame(Office=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=as.character(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('Republican', 'Democrat', 'Republican', 'Democrat',
                 'Republican', 'Democrat'),
         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

}

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.