Practice Activities for Lesson One

Basic Set Up For Practice Activities

This is a practice activity, mean to be completed individually. Please review the activities and attempt them on your own, without relying on the solutions below, or AI support.

1. Load the required libraries and set working directory

You will need to load the general libraries we have used up until this point: 1. dplyr 2. tidyr

AND
Ensure your working directory has been pointed to the correct location.

2. Load data

Assign objects to all these data frames below.

  1. All Client Services
  2. Active Clients
  3. CMA 2
  4. Demographics

3. Practice Activities

Code and responses are viewable if you scroll down.

Mathematical Operations

  1. Divide 1,234 by 10.46. Then, find the log of the response. remember, base R contains most arithmetic functions.
    Hint: if you don’t know how to code the functions, type ?Arithmetic or ?Math into the console below. The results will be listed in Help section on right.
  2. Find the square root of 608,998.91. Assign that value to an object called x. Then, round to the to only 2 decimal places behind the period.
  3. Find the max value in the column ‘EarningsAmount’, from the dataframe ’cm2. Then find the minimum value in that same value. Finally, find the average value in that column. Remember to warn R to ignore NA’s with the command: rm = TRUE.
    Hint: *Ensure that your column is in the correct class (numeric) to be able to complete mathematic operations.

Enough math! Let’s move on…

Describe the data

  1. What type of object is the cm2 data frame?
  2. How many rows are in the Active Clients data frame?
  3. In cm2, name 3 columns of a character class, 3 columns of a numeric class, and one column of a logical class.

Subset the data

  1. Using base R, make a new object called df_1 built from the data frame ‘active’. Only retain columns 1-3,and 5. Only retain rows 2-25. Now, tell me its dimensions.
    8.Using the dpylr package, look at the following columns in cm2: Program Name, Income Adequacy and Income Adequacy Rent. Copy and paste the first 3 rows you see.
  2. Create a new data frame (df_2) froms dems and using dplyr, build it out of columns 1-4, 8, 11, and 25.

Responses with code

Load libraries and set working directory

#setwd("~/Directories/Practice Directory") ### set to your appropriate directory

library(readr)
library(tidyr)
library(dplyr)

Read in the data and assign to objects

cm2<- read_csv("cm2.csv")
dems <- read_csv("dems.csv")
active<- read_csv("active.1yr.csv")
acs<- read_csv("acs.IG.csv")

Practice Activities: Mathematical Operations

  1. Divide 1,234 by 10.46. Then, find the log of the response. remember, base R contains most arithmetic functions.
    Hint: if you don’t know how to code the functions, type ?Arithmetic or ?Math into the console below. The results will be listed in Help section on right.
1234/10.46
## [1] 117.9732
log(117.9732) ### log() is one of many arithmetic functions in base R
## [1] 4.770457
  1. Find the square root of 608,998.91. Assign that value to an object called x. Then, round to the to only 2 spaces behind the decimal.
x<- sqrt(608998.91) ## do this in one command for efficiency, versus finding the square root and then manually writing it into the next command
round(x, digits = 2) ## use round() to round a value, and set exactly the amount of spaces 
## [1] 780.38
  1. Find the max value in the column ‘EarningsAmount’, from the dataframe ’cm2. Then find the minimum value in that same value. Finally, find the average value in that column. Remember to warn R to ignore NA’s with the command: na.rm = TRUE.
    Hint: *Ensure that your column is in the correct class (numeric) to be able to complete mathematic operations.
class(cm2$EarningsAmount) ## check the class; if its good to go, no changes needed
## [1] "numeric"
max(cm2$EarningsAmount, na.rm=TRUE)
## [1] 7200
min(cm2$EarningsAmount, na.rm=TRUE)
## [1] 0
mean(cm2$EarningsAmount,na.rm=TRUE)
## [1] 1172.132

Practice Activities: Describe the data

  1. What type of object is the cm2 data frame?
class(cm2)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"
  1. How many rows are in the Active Clients data frame?
nrow(active)
## [1] 831
  1. In cm2, name 3 columns of a character class, 3 columns of a numeric class, and one column of a logical class.
str(cm2)## Note we could use summary(), but str() lists the variables so its easier to view

Practice Activities: Subset the data

  1. Using base R, make a new object called df_1 built from the data frame ‘active’. Only retain columns 1-3,and 5. Remove rows 6, 8 and 9-11
df_1<- active[-c(6, 8, 9:11), c(1:3,5)]

dim(df_1)
## [1] 826   4

8.Using the dpylr package, look at the following columns in cm2: Program Name, Income Adequacy and Income Adequacy Rent. Copy and paste the first 3 rows you see.

cm2 |> select(ProgramName, IncomeAdequacy, IncomeAdequacyRent)
## # A tibble: 397 × 3
##    ProgramName   IncomeAdequacy                               IncomeAdequacyRent
##    <chr>         <chr>                                        <chr>             
##  1 Caminos       Income is Sufficient and Well-Managed; Has … Spends about 30% …
##  2 Journeys      Can Meet Basic Needs with Subsidy; Appropri… <NA>              
##  3 Revive        Can Meet Basic Needs with Subsidy; Appropri… <NA>              
##  4 Revive        Can Meet Basic Needs with Subsidy; Appropri… <NA>              
##  5 Revive        No Income; No Benefits; No Subsidies         <NA>              
##  6 New Horizons  No Income; No Benefits; No Subsidies         <NA>              
##  7 SH Home       Can Meet Basic Needs with Subsidy; Appropri… <NA>              
##  8 Healthy Paths Inadequate Income and/or Spontaneous or Ina… <NA>              
##  9 Casa Alma     No Income; No Benefits; No Subsidies         <NA>              
## 10 Casa Alma     Can Meet Basic Needs with Subsidy; Appropri… <NA>              
## # ℹ 387 more rows
  1. Create a new data frame (df_2) from dems and using dplyr, build it out of columns 1-4, 8, 11, and 25.
names(dems)
##  [1] "ParticipantID"                    "DateOfBirth"                     
##  [3] "ChartNumberACAC"                  "EnglishFluencyReading"           
##  [5] "EnglishFluencySpoken"             "EnglishFluencyWritten"           
##  [7] "Ethnicity"                        "EthnicitySpecify"                
##  [9] "Latino"                           "Gender"                          
## [11] "GenderIDSpecify"                  "AssignedSex"                     
## [13] "GenderSpecify"                    "SexualOrientation"               
## [15] "OrientationSpecify"               "MaritalStatus"                   
## [17] "Veteran"                          "VeteranService"                  
## [19] "ActiveCombat"                     "Language"                        
## [21] "LanguageSpecify"                  "BirthCity"                       
## [23] "BirthState"                       "BirthCountry"                    
## [25] "FosterCareCounty"                 "Staff"                           
## [27] "Address"                          "Address2"                        
## [29] "Zip"                              "Email"                           
## [31] "Cell Phone"                       "Work Phone"                      
## [33] "Work Phone Extension"             "ONESystemID"                     
## [35] "Suffix"                           "Preferred Name(s)"               
## [37] "Pronouns"                         "Mother's Maiden Name"            
## [39] "Alert"                            "Home Phone"                      
## [41] "Type of Residence"                "Referring Agency (Routz)"        
## [43] "Referred from HIV Test Provider?" "Chart Number"                    
## [45] "Phonetic Spelling"
df_2<- dems |> select(ParticipantID:EnglishFluencyReading, EthnicitySpecify, GenderIDSpecify,FosterCareCounty)