0

I have a table of pending bills in the Scottish Parliament. One of the columns (BillTypeID) is populated with numbers that indicate what type of bill each one is (there are seven different types of bills).

I have another table that describes which number corresponds to which bill types ( 1 = "Executive", 2 = "Member's", etc.)

I want to replace the number in my main table with the corresponding string that describes the type for each bill.

Data:

bills <- jsonlite::fromJSON(url("https://data.parliament.scot/api/bills"))
bill_stages <- jsonlite::fromJSON(url("https://data.parliament.scot/api/billstages"))
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116

1 Answers1

2

This is probably a duplicate but I can't find the corresponding answer ...

The easiest way to do this is with merge().

d1 <- data.frame(billtype=c(1,1,3,3),
          bill=c("first","second","third","fourth"))
d2 <- data.frame(billtype=c(1,2,3),
                 billtypename=c("foo","bar","bletch"))
d3 <- merge(d1,d2)
## 
##   billtype   bill billtypename
## 1        1  first          foo
## 2        1 second          foo
## 3        3  third       bletch
## 4        3 fourth       bletch

... then drop the billtype column if you don't want it any more. You can probably do it slightly more efficiently with match() (see my answer to the linked question).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453