Foramtting Output

Loading some packages:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(modeldata)
library(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
ames |> 
  group_by(Sale_Condition) |> 
  summarize(MeanSalePrice = mean(Sale_Price), NumberOfSales = n())
# A tibble: 6 × 3
  Sale_Condition MeanSalePrice NumberOfSales
  <fct>                  <dbl>         <int>
1 Abnorml              140396.           190
2 AdjLand              108917.            12
3 Alloca               161844.            24
4 Family               157489.            46
5 Normal               175568.          2413
6 Partial              273374.           245
ames |> 
  group_by(Sale_Condition) |> 
  summarize(MeanSalePrice = scales::dollar(mean(Sale_Price)), 
            NumberOfSales = scales::comma(n()))
# A tibble: 6 × 3
  Sale_Condition MeanSalePrice NumberOfSales
  <fct>          <chr>         <chr>        
1 Abnorml        $140,396      190          
2 AdjLand        $108,917      12           
3 Alloca         $161,844      24           
4 Family         $157,489      46           
5 Normal         $175,568      2,413        
6 Partial        $273,374      245          

Better yet:

ames |> 
  group_by(Sale_Condition) |> 
  summarize(MeanSalePrice = scales::dollar(mean(Sale_Price)), 
            NumberOfSales = scales::comma(n())) |> 
  knitr::kable()
Sale_Condition MeanSalePrice NumberOfSales
Abnorml $140,396 190
AdjLand $108,917 12
Alloca $161,844 24
Family $157,489 46
Normal $175,568 2,413
Partial $273,374 245
ames |> 
  group_by(Sale_Condition) |> 
  summarize(MeanSalePrice = scales::dollar(mean(Sale_Price)), 
            NumberOfSales = scales::comma(n())) -> T1
T1 |> 
  knitr::kable(align = c("r", "r", "r"),
               col.names = c("Sale Condition", "Mean Sale Price", "Number of Sales"))
Sale Condition Mean Sale Price Number of Sales
Abnorml $140,396 190
AdjLand $108,917 12
Alloca $161,844 24
Family $157,489 46
Normal $175,568 2,413
Partial $273,374 245

Using inline R code to answer questions such as: “How many houses had a sale condition of”Normal” and what was their average sale price?

There were 2,413 houses sold in Ames with a "Normal" sale condition for and their average sale price was $175,568.