#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)
)
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
#Basic information about data frame
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
##
dim(hospital)
## [1] 5 7
ncol(hospital)
## [1] 7
nrow(hospital)
## [1] 5
names(hospital)
## [1] "PatientID" "PatientName" "Gender" "Ward"
## [5] "TreatmentCost" "MedicineCost" "DaysAdmitted"
#Accessing data
hospital$PatientID
## [1] 101 102 103 104 105
hospital[,2]
## [1] "Aman" "Riya" "Karan" "Neha" "Rohit"
hospital[,c("Gender","TreatmentCost")]
## Gender TreatmentCost
## 1 Male 12000
## 2 Female 25000
## 3 Male 15000
## 4 Female 30000
## 5 Male NA
hospital[3,6]
## [1] 4000
#Access column data using $ operator
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
#Add a new column
hospital$TotalBill <- hospital$TreatmentCost + 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 NA
#Calculate total for a specific row(row number)
hospital$TotalBill[2]<-
hospital$TreatmentCost[2] + hospital$MedicineCost[2]
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 NA
#Descending order of totalbill
hospital[order(-hospital$TotalBill), ]
## PatientID PatientName Gender Ward TreatmentCost MedicineCost DaysAdmitted
## 4 104 Neha Female Private 30000 6000 6
## 2 102 Riya Female ICU 25000 5000 5
## 3 103 Karan Male General 15000 4000 2
## 1 101 Aman Male General 12000 3000 3
## 5 105 Rohit Male ICU NA 4500 4
## TotalBill
## 4 36000
## 2 30000
## 3 19000
## 1 15000
## 5 NA
#Ascending order of totalbill
hospital[order(hospital$TotalBill), ]
## PatientID PatientName Gender Ward TreatmentCost MedicineCost DaysAdmitted
## 1 101 Aman Male General 12000 3000 3
## 3 103 Karan Male General 15000 4000 2
## 2 102 Riya Female ICU 25000 5000 5
## 4 104 Neha Female Private 30000 6000 6
## 5 105 Rohit Male ICU NA 4500 4
## TotalBill
## 1 15000
## 3 19000
## 2 30000
## 4 36000
## 5 NA