This is Xhulia Turkaj’s R Markdown document for Week 2 Assignment on the R Bridge Course for CUNY SPS MSDS 2023 using the Australian Health Service Utilization Data.
week_2_data <- read.csv("https://raw.githubusercontent.com/xhuliaturkaj/BridgeMSDS/main/DoctorVisits.csv", header = TRUE, row.names = 1)
head(week_2_data, 10)
## visits gender age income illness reduced health private freepoor freerepat
## 1 1 female 0.19 0.55 1 4 1 yes no no
## 2 1 female 0.19 0.45 1 2 1 yes no no
## 3 1 male 0.19 0.90 3 0 0 no no no
## 4 1 male 0.19 0.15 1 0 0 no no no
## 5 1 male 0.19 0.45 2 5 1 no no no
## 6 1 female 0.19 0.35 5 1 9 no no no
## 7 1 female 0.19 0.55 4 0 2 no no no
## 8 1 female 0.19 0.15 3 0 6 no no no
## 9 1 female 0.19 0.65 2 0 5 yes no no
## 10 1 male 0.19 0.15 1 0 0 yes no no
## nchronic lchronic
## 1 no no
## 2 no no
## 3 no no
## 4 no no
## 5 yes no
## 6 yes no
## 7 no no
## 8 no no
## 9 no no
## 10 no no
Question 1 - 1. Use the summary function to gain an overview of the data set. Then display the mean and median for at least two attributes of your data.
summary(week_2_data)
## visits gender age income
## Min. :0.0000 Length:5190 Min. :0.1900 Min. :0.0000
## 1st Qu.:0.0000 Class :character 1st Qu.:0.2200 1st Qu.:0.2500
## Median :0.0000 Mode :character Median :0.3200 Median :0.5500
## Mean :0.3017 Mean :0.4064 Mean :0.5832
## 3rd Qu.:0.0000 3rd Qu.:0.6200 3rd Qu.:0.9000
## Max. :9.0000 Max. :0.7200 Max. :1.5000
## illness reduced health private
## Min. :0.000 Min. : 0.0000 Min. : 0.000 Length:5190
## 1st Qu.:0.000 1st Qu.: 0.0000 1st Qu.: 0.000 Class :character
## Median :1.000 Median : 0.0000 Median : 0.000 Mode :character
## Mean :1.432 Mean : 0.8619 Mean : 1.218
## 3rd Qu.:2.000 3rd Qu.: 0.0000 3rd Qu.: 2.000
## Max. :5.000 Max. :14.0000 Max. :12.000
## freepoor freerepat nchronic lchronic
## Length:5190 Length:5190 Length:5190 Length:5190
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
Australian Health Service Utilization
Cross-section data originating from the 1977–1978 Australian Health Survey.
The variables of the dataframe are as listed
visits - Number of doctor visits in past 2 weeks.
gender - Factor indicating gender.
age - Age in years divided by 100.
income - Annual income in tens of thousands of dollars.
illness - Number of illnesses in past 2 weeks.
reduced - Number of days of reduced activity in past 2 weeks due to illness or injury.
health - General health questionnaire score using Goldberg’s method.
private - Factor. Does the individual have private health insurance?
freepoor - Factor. Does the individual have free government health insurance due to low income?
freerepat - Factor. Does the individual have free government health insurance due to old age, disability or veteran status?
nchronic - Factor. Is there a chronic condition not limiting activity?
lchronic Factor. Is there a chronic condition limiting activity?
The variables: gender, private, freepoor, freerepat and chronic are are listed as character variables however I believe it is best to represent them as binary categorical variables as they are dichotomous categorical variables.
week_2_data$gender <- factor(week_2_data$gender)
week_2_data$private <- factor(week_2_data$private)
week_2_data$freepoor <- factor(week_2_data$freepoor)
week_2_data$freerepat <- factor(week_2_data$freerepat)
week_2_data$nchronic <- factor(week_2_data$nchronic)
week_2_data$lchronic <- factor(week_2_data$lchronic)
mean(week_2_data$visits, na.rm = TRUE)
## [1] 0.3017341
median(week_2_data$visits, na.rm = TRUE)
## [1] 0
mean(week_2_data$illness, na.rm = TRUE)
## [1] 1.431985
median(week_2_data$illness, na.rm = TRUE)
## [1] 1
Question 2. Create a new data frame with a subset of the columns AND rows. There are several ways to do this so feel free to try a couple if you want. Make sure to rename the new data set so it simply just doesn’t write it over.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
selected_rows <- c(150:250, 300:350, 500:650)
subset_doctors_visits <- week_2_data %>%
slice(selected_rows) %>%
select(visits, illness, private, freepoor, freerepat, health)
head(subset_doctors_visits, 15)
## visits illness private freepoor freerepat health
## 1 5 1 no no no 0
## 2 1 1 no no no 0
## 3 1 2 yes no no 1
## 4 2 5 no no yes 3
## 5 1 3 no yes no 0
## 6 1 2 no yes no 0
## 7 1 5 yes no no 5
## 8 1 1 yes no no 0
## 9 1 5 yes no no 1
## 10 1 2 no no no 0
## 11 1 4 yes no no 1
## 12 2 2 yes no no 2
## 13 1 1 no no no 0
## 14 1 1 no no no 0
## 15 2 1 yes no no 0
Question 3. 3. Create new column names for each column in the new data frame created in step 2.
names(subset_doctors_visits) <- c("doc_visits", "health_issues", "private_insurance", "free_gov_insurance", "elderly_free_gov_insurance", "overall_health")
head(subset_doctors_visits, 10)
## doc_visits health_issues private_insurance free_gov_insurance
## 1 5 1 no no
## 2 1 1 no no
## 3 1 2 yes no
## 4 2 5 no no
## 5 1 3 no yes
## 6 1 2 no yes
## 7 1 5 yes no
## 8 1 1 yes no
## 9 1 5 yes no
## 10 1 2 no no
## elderly_free_gov_insurance overall_health
## 1 no 0
## 2 no 0
## 3 no 1
## 4 yes 3
## 5 no 0
## 6 no 0
## 7 no 5
## 8 no 0
## 9 no 1
## 10 no 0
Question 4. Use the summary function to create an overview of your new data frame created in step 2. The print the mean and median for the same two attributes. Please compare (i.e. tell me how the values changed and why).
summary(subset_doctors_visits)
## doc_visits health_issues private_insurance free_gov_insurance
## Min. :1.000 Min. :0.000 no :154 no :295
## 1st Qu.:1.000 1st Qu.:1.000 yes:149 yes: 8
## Median :1.000 Median :2.000
## Mean :1.512 Mean :2.188
## 3rd Qu.:2.000 3rd Qu.:3.000
## Max. :8.000 Max. :5.000
## elderly_free_gov_insurance overall_health
## no :232 Min. : 0.000
## yes: 71 1st Qu.: 0.000
## Median : 1.000
## Mean : 2.178
## 3rd Qu.: 3.000
## Max. :12.000
mean(subset_doctors_visits$doc_visits, na.rm = TRUE)
## [1] 1.511551
median(subset_doctors_visits$doc_visits, na.rm = TRUE)
## [1] 1
mean(subset_doctors_visits$health_issues, na.rm = TRUE)
## [1] 2.188119
median(subset_doctors_visits$health_issues, na.rm = TRUE)
## [1] 2
Due to the subseting of the data frame the mean and median of my chosen variables has changed drastically. Because I didn’t subset using a proper random sampling tenchnique the mean of the number of doctors’ visits has increased drastically going from 0.3 to 1.5. The median of the number of doctors visits has gone from 0 to 1.
For the varibale health issues which indicates the nr of health concerns an individual has experienced the last 2 weeks the mean has gone from 1.4 to 2.1 and the median has gone from 1 to 2.
This suggests that the subset I selected might contain a higher proportion of individuals with more doctors’ visits and more health concerns compared to the original dataset. As a result, the mean and median have shifted towards higher values.
0.3017341 [1] 0 [1] 1.431985 [1] 1
Question 5. 5 For at least 3 different/distinct values in a column please rename so that every value in that column is renamed. For example, change the letter “e” to “excellent”, the letter “a” to “average’ and the word “bad” to “terrible”.
library(dplyr)
subset_doctors_visits_final_version <- subset_doctors_visits %>%
mutate(
private_insurance = case_when(
private_insurance == 'yes' ~ 'Has Private Insurance',
private_insurance == 'no' ~ 'Does Not Have Private Insurance',
TRUE ~ private_insurance
),
free_gov_insurance = case_when(
free_gov_insurance == 'yes' ~ 'Has Free Gov Insurance',
free_gov_insurance == 'no' ~ 'Does Not Have Free Gov Insurance',
TRUE ~ free_gov_insurance
),
elderly_free_gov_insurance = case_when(
elderly_free_gov_insurance == 'yes' ~ 'Has Free For Elderly Gov Insurance',
elderly_free_gov_insurance == 'no' ~ 'Does no Have Free For Elderly Gov Insurance',
TRUE ~ elderly_free_gov_insurance
)
)
head(subset_doctors_visits_final_version, 10)
## doc_visits health_issues private_insurance
## 1 5 1 Does Not Have Private Insurance
## 2 1 1 Does Not Have Private Insurance
## 3 1 2 Has Private Insurance
## 4 2 5 Does Not Have Private Insurance
## 5 1 3 Does Not Have Private Insurance
## 6 1 2 Does Not Have Private Insurance
## 7 1 5 Has Private Insurance
## 8 1 1 Has Private Insurance
## 9 1 5 Has Private Insurance
## 10 1 2 Does Not Have Private Insurance
## free_gov_insurance elderly_free_gov_insurance
## 1 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 2 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 3 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 4 Does Not Have Free Gov Insurance Has Free For Elderly Gov Insurance
## 5 Has Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 6 Has Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 7 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 8 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 9 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## 10 Does Not Have Free Gov Insurance Does no Have Free For Elderly Gov Insurance
## overall_health
## 1 0
## 2 0
## 3 1
## 4 3
## 5 0
## 6 0
## 7 5
## 8 0
## 9 1
## 10 0