-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathFaizan_khi_R_Assignment2.R
More file actions
133 lines (110 loc) · 3.03 KB
/
Faizan_khi_R_Assignment2.R
File metadata and controls
133 lines (110 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
library(dplyr)
library(readr)
library(tidyr)
library(lubridate)
Hospital_data <- read.csv("E:/DIH/RWork/Assignment/hospitaldata.csv",header = TRUE, stringsAsFactors = FALSE)
#Q1
#View(Hospital_data)
names(Hospital_data) <- gsub("\\." ," ", names(Hospital_data))
#View(Hospital_data)
#Q3
Hospital_data$Age<-gsub("M" ,"", Hospital_data$Age)
Hospital_data$Age <- as.numeric(Hospital_data$Age)
mean(Hospital_data$Age,na.rm=TRUE)
#Q4
length(which(Hospital_data$Age <= 12 & Hospital_data$Age >=1))
#Q5
Hospital_data$Sex<-gsub("f" ,"F", Hospital_data$Sex)
Hospital_data%>%
filter(Sex=='M')%>%
group_by(Procedure)%>%
summarize(maxi=n())%>%
filter(maxi==max(maxi))%>%
print
Hospital_data%>%
filter(Sex=='F')%>%
group_by(Procedure)%>%
summarize(maxi=n())%>%
filter(maxi==max(maxi))%>%
print
#Q6
Hospital_data$`Total Charges` <- as.numeric(Hospital_data$`Total Charges`)
Hospital_data%>%
group_by(`Consulting Doctor`)%>%
summarize(high=sum(`Total Charges`, na.rm=TRUE))%>%
filter(high==max(high))%>%
print
#Q7
Hospital_data$`Total Charges` <- as.numeric(Hospital_data$`Total Charges`)
Hospital_data%>%
group_by(Procedure)%>%
summarize(high=sum(`Total Charges`, na.rm=TRUE))%>%
filter(high==max(high))%>%
print
#Q8
Hospital_data$Time<-hour(strptime(Hospital_data$Time, "%I:%M %p" ))
Hospital_data$Time
Hospital_data %>%
filter(!is.na(Time), Time != '-') %>%
group_by(Time) %>%
summarize(timeCount = n()) %>%
filter(Time != '') %>%
filter(timeCount == max(timeCount))
#Q10
Hospital_data%>%
group_by(id)%>%
summarize(repeat_id=n())%>%
filter(repeat_id>1)%>%
nrow()%>%
print
#Q11
Hospital_data%>%
group_by(id)%>%
summarize(repeat_id=n())%>%
filter(repeat_id>1)%>%
print
#Q12
Hospital_data%>%
group_by(id,Procedure)%>%
summarize(repeat_id=n())%>%
filter(repeat_id>1)%>%
print
#Q13
Hospital_data%>%
filter(Sex=='M')%>%
select(Age)%>%
summarize(med=median(Age,na.rm=TRUE))%>%
print
Hospital_data%>%
filter(Sex=='F')%>%
select(Age)%>%
summarize(med=median(Age,na.rm=TRUE))%>%
print
#Q14
Hospital_data$`Amount Balance`<-gsub("," ,"",Hospital_data$`Amount Balance` ,fixed=TRUE)
Hospital_data$`Amount Balance`<-gsub(".00" ,"", Hospital_data$`Amount Balance`,fixed=TRUE)
Hospital_data$`Amount Balance` <- as.numeric(Hospital_data$`Amount Balance`)
Hospital_data%>%
select(`Amount Balance`)%>%
summarize(sum1=sum(`Amount Balance`, na.rm=TRUE))%>%
print
#Q15
Hospital_data%>%
filter(Procedure=='Consultation')%>%
select(`Total Charges`)%>%
summarize(maxi=sum(`Total Charges`,na.rm=TRUE) )%>%
print
#Q17
Hospital_data %>%
filter(!is.na(Age), Age!='-') %>%
group_by(Age) %>%
summarize(AgeCount=n()) %>%
filter(Age!='') %>%
filter(AgeCount == max(AgeCount))%>%
print
#Q18
Hospital_data%>%
filter(Procedure=='X Ray'| Procedure=='Scalling')%>%
select(`Total Charges`)%>%
summarize(maxi=sum(`Total Charges`,na.rm=TRUE) )%>%
print