# Function: s_apply: Applies a function to a list of dataframes
```{r}
# Data
mtcars
df_mtcars = mtcars # rename to remind ourselves that it is a df
df_mtcars
class(df_mtcars)

# Data Split
# Split the dataframe by cylinder count
# into a list of dataframes, 1 for each cylinder count
list_df_mtcars_split_cyl <- split(df_mtcars, df_mtcars$cyl)
list_df_mtcars_split_cyl
class(list_df_mtcars_split_cyl)

# Define function that operates on a dataframe
# input  = df
# output = integer
f_n_rows <- function(df) {
  nrow(df)
}

# Test the function on 1 dataframe
i_n_rows_mtcars = f_n_rows(df_mtcars)
i_n_rows_mtcars
class(i_n_rows_mtcars)

# Apply the function on MANY dataframes (in a list)
num_cars_by_num_cyl <- sapply(list_df_mtcars_split_cyl, f_n_rows)
num_cars_by_num_cyl
class(num_cars_by_num_cyl)

# Convert the output into a dataframe for clarity
df_num_cars_by_num_cyl <- data.frame(num_cars_by_num_cyl)
df_num_cars_by_num_cyl
class(df_num_cars_by_num_cyl)
```
How does sapply work?

Copy + Paste 
the code on the left
into any .Rmd file

Ctrl + Enter
each line from the top down
See the output
Understand
> df_num_cars_by_num_cyl
  num_cars_by_num_cyl
4                  11
6                   7
8                  14

> class(df_num_cars_by_num_cyl)
[1] "data.frame"