# 1. Create Hospital Data Frame (Mixed Data Types)
# ----------------------------------------------------------
hospital <- data.frame(
PatientID = c(101, 102, 103, 104, 105),
PatientName = c("Aman", "Riya", "Karan", "Neha", "Rohit"),
Gender = factor(c("Male", "Female", "Male", "Female", "Male")),
Ward = factor(c("General", "ICU", "General", "Private", "ICU")),
TreatmentCost = c(12000, 25000, 15000, 30000, NA),
MedicineCost = c(3000, 5000, 4000, 6000, 4500),
DaysAdmitted = c(3, 5, 2, 6, 4)
)
print(hospital)
## PatientID PatientName Gender Ward TreatmentCost MedicineCost DaysAdmitted
## 1 101 Aman Male General 12000 3000 3
## 2 102 Riya Female ICU 25000 5000 5
## 3 103 Karan Male General 15000 4000 2
## 4 104 Neha Female Private 30000 6000 6
## 5 105 Rohit Male ICU NA 4500 4
str(hospital)
## 'data.frame': 5 obs. of 7 variables:
## $ PatientID : num 101 102 103 104 105
## $ PatientName : chr "Aman" "Riya" "Karan" "Neha" ...
## $ Gender : Factor w/ 2 levels "Female","Male": 2 1 2 1 2
## $ Ward : Factor w/ 3 levels "General","ICU",..: 1 2 1 3 2
## $ TreatmentCost: num 12000 25000 15000 30000 NA
## $ MedicineCost : num 3000 5000 4000 6000 4500
## $ DaysAdmitted : num 3 5 2 6 4
summary(hospital)
## PatientID PatientName Gender Ward TreatmentCost
## Min. :101 Length:5 Female:2 General:2 Min. :12000
## 1st Qu.:102 Class :character Male :3 ICU :2 1st Qu.:14250
## Median :103 Mode :character Private:1 Median :20000
## Mean :103 Mean :20500
## 3rd Qu.:104 3rd Qu.:26250
## Max. :105 Max. :30000
## NA's :1
## MedicineCost DaysAdmitted
## Min. :3000 Min. :2
## 1st Qu.:4000 1st Qu.:3
## Median :4500 Median :4
## Mean :4500 Mean :4
## 3rd Qu.:5000 3rd Qu.:5
## Max. :6000 Max. :6
##
hospital$PatientName
## [1] "Aman" "Riya" "Karan" "Neha" "Rohit"
hospital$TreatmentCost
## [1] 12000 25000 15000 30000 NA
hospital[, c("TreatmentCost", "MedicineCost")]
## TreatmentCost MedicineCost
## 1 12000 3000
## 2 25000 5000
## 3 15000 4000
## 4 30000 6000
## 5 NA 4500
hospital$TotalBill <-
ifelse(is.na(hospital$TreatmentCost), 0, hospital$TreatmentCost) +
ifelse(is.na(hospital$MedicineCost), 0, hospital$MedicineCost)
hospital
## PatientID PatientName Gender Ward TreatmentCost MedicineCost DaysAdmitted
## 1 101 Aman Male General 12000 3000 3
## 2 102 Riya Female ICU 25000 5000 5
## 3 103 Karan Male General 15000 4000 2
## 4 104 Neha Female Private 30000 6000 6
## 5 105 Rohit Male ICU NA 4500 4
## TotalBill
## 1 15000
## 2 30000
## 3 19000
## 4 36000
## 5 4500
new_patient <- data.frame(
PatientID = 106,
PatientName = "Simran",
Gender = factor("Female", levels = levels(hospital$Gender)),
Ward = factor("Private", levels = levels(hospital$Ward)),
TreatmentCost = 20000,
MedicineCost = 5500,
DaysAdmitted = 3
)
new_patient$TotalBill <- new_patient$TreatmentCost + new_patient$MedicineCost
hospital <- rbind(hospital, new_patient)
hospital
## PatientID PatientName Gender Ward TreatmentCost MedicineCost DaysAdmitted
## 1 101 Aman Male General 12000 3000 3
## 2 102 Riya Female ICU 25000 5000 5
## 3 103 Karan Male General 15000 4000 2
## 4 104 Neha Female Private 30000 6000 6
## 5 105 Rohit Male ICU NA 4500 4
## 6 106 Simran Female Private 20000 5500 3
## TotalBill
## 1 15000
## 2 30000
## 3 19000
## 4 36000
## 5 4500
## 6 25500