library(tidyverse)
library(ggplot2)
library(ggthemes)
library(dplyr)
library(anytime)
library(knitr)
library(kableExtra)
library(sparkline)
library(DT)TABLE ASSIGNMENT
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
Description of the data and data source
For this assignment we use the data produces by the National Association of Realtors (NAR). NAR produces housing statistics on the national, regional, and metro-marketlevel where data is available. we use the national historical data (view US data) on https://www.realtor.com/research/data/ (Data from 2016 to 2022)
what you want to convey in the table
We are going to make a table that shows the median listing price, the median square feet, the price increased count, price reduced count, and the average price by state.
what functionality you put into the table and why For the functionalty, we make a Tables with kable and DT. we also add Monthly patterns in Ohio State.
what formatting you put into the table and why kable_styling, kable_paper are the only use in our table.
library(tidyverse)
house=read.csv("C:/Users/L/OneDrive/Bureau/STAA 566/TABLE/RDC_Inventory_Core_Metrics_State_History.csv",header = TRUE)
house=house%>% rename(Date=month_date_yyyymm)
house$Date=anydate(house$Date)
house$Date=as.Date(house$Date,"%y-%m-%d")
house=house %>% drop_na()Make a Table with kbl
cases <- house%>%
filter(Date == max(Date)) %>%
drop_na()
# Select variable that going to be use for the table.
house_table_1 = cases %>%
select(state, median_listing_price,median_square_feet,price_increased_count,price_reduced_count,average_listing_price)
# make table
Table_1 = house_table_1 %>%
select(state, median_listing_price,price_increased_count,price_reduced_count,average_listing_price, median_square_feet) %>%
kbl(escape = FALSE,
col.names = c("State",
"Median price",
"Price increased",
"Price reduced",
"Average Price",
"Median feet"),
align = c("l","r","r","r","r","r"),
digits = 1) %>%
add_header_above(c(" ","Price Evolution" = 3, "Square Feet Evolution" = 2)) %>%
kable_styling(font_size=14, fixed_thead = TRUE)
Table_1 %>% kable_paper(lightable_options = "hover", full_width = FALSE)| State | Median price | Price increased | Price reduced | Average Price | Median feet |
|---|---|---|---|---|---|
| alaska | 399700 | 40 | 784 | 465011 | 1900 |
| alabama | 325000 | 348 | 4604 | 426049 | 2005 |
| arkansas | 274500 | 112 | 2628 | 375364 | 1887 |
| arizona | 492000 | 568 | 17224 | 678743 | 1895 |
| california | 725000 | 1616 | 27988 | 1297386 | 1756 |
| colorado | 609000 | 364 | 11368 | 1088739 | 2249 |
| connecticut | 500000 | 80 | 1792 | 1199332 | 2113 |
| district of columbia | 599450 | 16 | 464 | 888295 | 1213 |
| delaware | 467000 | 184 | 688 | 580810 | 2171 |
| florida | 477000 | 2444 | 38336 | 916818 | 1740 |
| georgia | 399950 | 1292 | 20252 | 543587 | 2203 |
| hawaii | 879500 | 36 | 912 | 1613733 | 1298 |
| iowa | 279950 | 528 | 2836 | 331226 | 1611 |
| idaho | 572245 | 120 | 6040 | 841647 | 2121 |
| illinois | 299900 | 328 | 9196 | 436583 | 1695 |
| indiana | 275000 | 264 | 5880 | 344575 | 1935 |
| kansas | 279000 | 180 | 1796 | 366173 | 2015 |
| kentucky | 282450 | 124 | 3436 | 358553 | 1866 |
| louisiana | 288300 | 164 | 3972 | 378393 | 1919 |
| massachusetts | 677950 | 136 | 3256 | 1203262 | 1826 |
| maryland | 400000 | 648 | 4336 | 540716 | 1880 |
| maine | 389000 | 44 | 1200 | 567862 | 1680 |
| michigan | 275000 | 344 | 9652 | 385986 | 1629 |
| minnesota | 375000 | 180 | 5148 | 481725 | 1986 |
| missouri | 279950 | 288 | 5204 | 369032 | 1792 |
| mississippi | 277000 | 116 | 1728 | 347090 | 2049 |
| montana | 637450 | 32 | 1468 | 1081090 | 2138 |
| north carolina | 410640 | 636 | 10360 | 558855 | 2090 |
| north dakota | 289635 | 44 | 620 | 346422 | 2111 |
| nebraska | 329850 | 36 | 952 | 383791 | 2086 |
| new hampshire | 499450 | 36 | 884 | 669336 | 1969 |
| new jersey | 489950 | 404 | 5036 | 734871 | 1728 |
| new mexico | 375000 | 84 | 1424 | 555448 | 2016 |
| nevada | 485500 | 220 | 7892 | 743007 | 1859 |
| new york | 579500 | 520 | 8464 | 1234330 | 1623 |
| ohio | 232400 | 328 | 8348 | 319085 | 1716 |
| oklahoma | 280245 | 168 | 3780 | 379209 | 1869 |
| oregon | 552450 | 1444 | 6980 | 721979 | 1879 |
| pennsylvania | 289450 | 756 | 8680 | 412497 | 1729 |
| rhode island | 479450 | 12 | 472 | 807265 | 1600 |
| south carolina | 359450 | 728 | 6736 | 534512 | 1999 |
| south dakota | 355000 | 32 | 520 | 457537 | 1947 |
| tennessee | 432500 | 408 | 9836 | 609266 | 2087 |
| texas | 397500 | 2592 | 39344 | 546701 | 2082 |
| utah | 599900 | 336 | 7232 | 933727 | 2434 |
| virginia | 425492 | 452 | 6168 | 602607 | 2016 |
| vermont | 437000 | 12 | 356 | 670529 | 2033 |
| washington | 646450 | 508 | 8836 | 862161 | 2027 |
| wisconsin | 349900 | 108 | 3316 | 442877 | 1822 |
| west virginia | 217000 | 36 | 996 | 292698 | 1797 |
| wyoming | 450000 | 16 | 692 | 972443 | 2335 |
Table_1| State | Median price | Price increased | Price reduced | Average Price | Median feet |
|---|---|---|---|---|---|
| alaska | 399700 | 40 | 784 | 465011 | 1900 |
| alabama | 325000 | 348 | 4604 | 426049 | 2005 |
| arkansas | 274500 | 112 | 2628 | 375364 | 1887 |
| arizona | 492000 | 568 | 17224 | 678743 | 1895 |
| california | 725000 | 1616 | 27988 | 1297386 | 1756 |
| colorado | 609000 | 364 | 11368 | 1088739 | 2249 |
| connecticut | 500000 | 80 | 1792 | 1199332 | 2113 |
| district of columbia | 599450 | 16 | 464 | 888295 | 1213 |
| delaware | 467000 | 184 | 688 | 580810 | 2171 |
| florida | 477000 | 2444 | 38336 | 916818 | 1740 |
| georgia | 399950 | 1292 | 20252 | 543587 | 2203 |
| hawaii | 879500 | 36 | 912 | 1613733 | 1298 |
| iowa | 279950 | 528 | 2836 | 331226 | 1611 |
| idaho | 572245 | 120 | 6040 | 841647 | 2121 |
| illinois | 299900 | 328 | 9196 | 436583 | 1695 |
| indiana | 275000 | 264 | 5880 | 344575 | 1935 |
| kansas | 279000 | 180 | 1796 | 366173 | 2015 |
| kentucky | 282450 | 124 | 3436 | 358553 | 1866 |
| louisiana | 288300 | 164 | 3972 | 378393 | 1919 |
| massachusetts | 677950 | 136 | 3256 | 1203262 | 1826 |
| maryland | 400000 | 648 | 4336 | 540716 | 1880 |
| maine | 389000 | 44 | 1200 | 567862 | 1680 |
| michigan | 275000 | 344 | 9652 | 385986 | 1629 |
| minnesota | 375000 | 180 | 5148 | 481725 | 1986 |
| missouri | 279950 | 288 | 5204 | 369032 | 1792 |
| mississippi | 277000 | 116 | 1728 | 347090 | 2049 |
| montana | 637450 | 32 | 1468 | 1081090 | 2138 |
| north carolina | 410640 | 636 | 10360 | 558855 | 2090 |
| north dakota | 289635 | 44 | 620 | 346422 | 2111 |
| nebraska | 329850 | 36 | 952 | 383791 | 2086 |
| new hampshire | 499450 | 36 | 884 | 669336 | 1969 |
| new jersey | 489950 | 404 | 5036 | 734871 | 1728 |
| new mexico | 375000 | 84 | 1424 | 555448 | 2016 |
| nevada | 485500 | 220 | 7892 | 743007 | 1859 |
| new york | 579500 | 520 | 8464 | 1234330 | 1623 |
| ohio | 232400 | 328 | 8348 | 319085 | 1716 |
| oklahoma | 280245 | 168 | 3780 | 379209 | 1869 |
| oregon | 552450 | 1444 | 6980 | 721979 | 1879 |
| pennsylvania | 289450 | 756 | 8680 | 412497 | 1729 |
| rhode island | 479450 | 12 | 472 | 807265 | 1600 |
| south carolina | 359450 | 728 | 6736 | 534512 | 1999 |
| south dakota | 355000 | 32 | 520 | 457537 | 1947 |
| tennessee | 432500 | 408 | 9836 | 609266 | 2087 |
| texas | 397500 | 2592 | 39344 | 546701 | 2082 |
| utah | 599900 | 336 | 7232 | 933727 | 2434 |
| virginia | 425492 | 452 | 6168 | 602607 | 2016 |
| vermont | 437000 | 12 | 356 | 670529 | 2033 |
| washington | 646450 | 508 | 8836 | 862161 | 2027 |
| wisconsin | 349900 | 108 | 3316 | 442877 | 1822 |
| west virginia | 217000 | 36 | 996 | 292698 | 1797 |
| wyoming | 450000 | 16 | 692 | 972443 | 2335 |
Monthly patterns for Median listing price and Median square feet
N = house %>% group_by(Date)
cases_1 <- N%>%
filter(Date == max(Date)) %>%
drop_na()
cases_trend_1 = N%>%
filter(Date > max(Date)-31)
## Only in Ohio
co_recent_1 <- cases_trend_1 %>%
filter(state=="ohio")
## Monthly patterns in Ohio
par(mfrow=c(1,1))
p_listing <- ggplot(co_recent_1, aes(x=Date, y=median_listing_price)) +
geom_point() + geom_line() +
theme_tufte() +
ylab("Monthly count") +
ggtitle("Ohio Median listing price")
p_feet <- ggplot(co_recent_1, aes(x=Date, y=median_square_feet)) +
geom_point() + geom_line() +
theme_tufte() +
ylab("Monthly count") +
ggtitle("Ohio Median Square feet")
p_listing 
p_feet
cases_trend = house %>%
filter(Date > max(Date)-31)
# one row per state with more recent data
house_table = cases%>%
select(state, median_listing_price,median_square_feet,price_increased_count,price_reduced_count,average_listing_price)%>%
mutate(plt_MP=NA, plt_MF=NA) %>% # this adds a column to store the plot
as.data.frame()Table with DT
# make table
DT_table = house_table %>%
select(state, median_listing_price,price_increased_count,price_reduced_count,average_listing_price, median_square_feet) %>%
datatable(colnames = c("State","Median price", "Price increased", "Price reduced","Average Price","Median feet")) %>%
formatRound(columns = c(2,3,5,6),
digits = 1)
DT_table