class: center, middle, inverse, title-slide .title[ # Intro to
R
(Part 2) ] .author[ ### Prof. Weldzius ] .institute[ ### Villanova University ] --- <style type="text/css"> .small .remark-code { /*Change made here*/ font-size: 85% !important; } .tiny .remark-code { /*Change made here*/ font-size: 50% !important; } </style> --- # How `RStudio` Works -- - `RStudio` allows us to: -- 1. Write scripts 2. Run scripts 3. See results -- - It is **deeply interactive** -- - We can highlight a line and press `ctrl+enter` / `cmd+enter` and see the result -- - **We can even do this with single objects!** --- # Give it a try - Comment **everything** -- - Create two objects - `a` contains the product of 3 and 5 (`3*5`) - `b` contains five numbers `c(10,21,43,87,175)` -- - Now create object `c` which is `a - b` ``` r # INSERT CODE HERE ``` --- # Functions & Packages -- - What are `packages`? -- - Basically, **functions** that someone else wrote -- - `R` has many functions already installed -- - These are known as "base `R`" and contain many useful functions -- - For example, `sum()` will add up a vector of numbers -- ``` r sum(object1) ``` -- - Quiz: What does the `mean()` function do? The `median()`? The `range()`? -- - Other base `R` functions interact with other files -- - For example, `read.csv()` will load a `.csv` file -- - And there are MANY **MANY** more --- # Installing Packages -- - In addition to the functions included in base `R`, we want more -- - For this class, we want one called `tidyverse` -- - `tidyverse` contains many (hundreds?) of functions that make `R` easier -- - But it is NOT included in the base `R` set of functions -- - Therefore, we need to add it -- - Use the base `R` function `install.packages("[PACKAGE NAME]")` -- - Specifically, `install.packages("tidyverse")` --- # Requiring Packages -- - Once installed, a package will live somewhere on your computer -- - However, any new *instance* of `R` will not automatically load the packages -- - We need to `require()` them to tell `R` to load them -- - Alternatively, we can use `library()` (but it's the same result) -- - So load the `tidyverse` package with `require(tidyverse)` -- - NB: you need quotes for the `install.packages()` function... -- - i.e., `install.packages("tidyverse")` -- - but NOT for the `require()` function -- - i.e., `require(tidyverse)` --- # Loading Data -- - So you should be using `R` via `RStudio` with the `tidyverse` package loaded -- - Now let's load some data -- - You can save it locally from the course [webpage](https://rweldzius.github.io/PSC4175/downloads/) and then load it from your computer -- - This is how you should do this for your problem sets and your final project. For the former: download data from the course webpage. - Only change: use your file path to the data file, e.g., "janedoe/documents/Villanova/Fall2025/psc4175/data/sc\_debt.Rds" -- - Or you can load it directly from the internet with the `read_rds()` function from `tidyverse` -- - NB: `R` is an "object-oriented language" (OOL) -- - We **create** an "object" to store the data using a left-arrow: `<-` -- ``` r df<-read_rds("https://github.com/rweldzius/PSC4175/raw/main/static/data/sc_debt.Rds") ``` -- - url: https://github.com/rweldzius/PSC4175/raw/main/static/data/sc_debt.Rds --- # Loading Data -- - We now have the contents of `sc_debt.Rds` stored in the object `df` -- - This is a "tabular data frame", aka a `tibble` - **Rows** are observations - **Columns** are values -- - We can look at this object directly ``` r df ``` ``` ## # A tibble: 2,546 × 16 ## unitid instnm stabbr grad_debt_mdn control region preddeg ## <int> <chr> <chr> <int> <chr> <chr> <chr> ## 1 100654 Alaba… AL 33375 Public South… Bachel… ## 2 100663 Unive… AL 22500 Public South… Bachel… ## 3 100690 Amrid… AL 27334 Private South… Associ… ## 4 100706 Unive… AL 21607 Public South… Bachel… ## 5 100724 Alaba… AL 32000 Public South… Bachel… ## 6 100751 The U… AL 23250 Public South… Bachel… ## 7 100760 Centr… AL 12500 Public South… Associ… ## 8 100812 Athen… AL 19500 Public South… Bachel… ## 9 100830 Aubur… AL 24826 Public South… Bachel… ## 10 100858 Aubur… AL 21281 Public South… Bachel… ## # ℹ 2,536 more rows ## # ℹ 9 more variables: openadmp <int>, adm_rate <dbl>, ## # ccbasic <int>, sat_avg <int>, md_earn_wne_p6 <int>, ## # ugds <int>, costt4_a <int>, selective <dbl>, ## # research_u <dbl> ``` --- # Loading Data -- - Or we can look at its columns ``` r names(df) ``` ``` ## [1] "unitid" "instnm" "stabbr" ## [4] "grad_debt_mdn" "control" "region" ## [7] "preddeg" "openadmp" "adm_rate" ## [10] "ccbasic" "sat_avg" "md_earn_wne_p6" ## [13] "ugds" "costt4_a" "selective" ## [16] "research_u" ``` --- # Loading Data (*[Source](https://data.ed.gov/dataset/9dc70e6b-8426-4d71-b9d5-70ce6094a3f4/resource/658b5b83-ac9f-4e41-913e-9ba9411d7967/download/collegescorecarddatadictionary_01192021.xlsx)) -- <table class=" lightable-paper lightable-hover" style='font-size: 13px; color: black; font-family: "Arial Narrow", arial, helvetica, sans-serif; width: auto !important; margin-left: auto; margin-right: auto;'> <thead> <tr> <th style="text-align:left;"> Name </th> <th style="text-align:left;"> Definition </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> unitid </td> <td style="text-align:left;"> Unit ID </td> </tr> <tr> <td style="text-align:left;"> instnm </td> <td style="text-align:left;"> Institution Name </td> </tr> <tr> <td style="text-align:left;"> stabbr </td> <td style="text-align:left;"> State Abbreviation </td> </tr> <tr> <td style="text-align:left;"> grad_debt_mdn </td> <td style="text-align:left;"> Median Debt of Graduates </td> </tr> <tr> <td style="text-align:left;"> control </td> <td style="text-align:left;"> Control Public or Private </td> </tr> <tr> <td style="text-align:left;"> region </td> <td style="text-align:left;"> Census Region </td> </tr> <tr> <td style="text-align:left;"> preddeg </td> <td style="text-align:left;"> Predominant Degree Offered: Assocates or Bachelors </td> </tr> <tr> <td style="text-align:left;"> openadmp </td> <td style="text-align:left;"> Open Admissions Policy: 1=Yes, 2=No, 3=No 1st time students </td> </tr> <tr> <td style="text-align:left;"> adm_rate </td> <td style="text-align:left;"> Admissions Rate: proportion of applications accepted </td> </tr> <tr> <td style="text-align:left;"> ccbasic </td> <td style="text-align:left;"> Type of institution* </td> </tr> <tr> <td style="text-align:left;"> sat_avg </td> <td style="text-align:left;"> Average SAT scores </td> </tr> <tr> <td style="text-align:left;"> md_earn_wne_p6 </td> <td style="text-align:left;"> Average Earnings of Recent Graduates </td> </tr> <tr> <td style="text-align:left;"> ugds </td> <td style="text-align:left;"> Number of undergraduates </td> </tr> <tr> <td style="text-align:left;"> costt4_a </td> <td style="text-align:left;"> Average cost of attendance (tuition-grants) </td> </tr> <tr> <td style="text-align:left;"> selective </td> <td style="text-align:left;"> Institution admits fewer than 10% of applications, 1=Yes, 0=No </td> </tr> <tr> <td style="text-align:left;"> research_u </td> <td style="text-align:left;"> Institution is a research university, 1=Yes, 0=No </td> </tr> </tbody> </table>