-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionList.Rmd
More file actions
159 lines (133 loc) · 4.82 KB
/
FunctionList.Rmd
File metadata and controls
159 lines (133 loc) · 4.82 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
```{r}
#zip codes for state
data(zipcode)
#takes a value for state and returns all the zipcodes from the state
zips_from_state<-function(state_name){
zip_holder<-zipcode%>%
filter(state==state_name)%>%
select(zip)
zip_state<-zip_holder[,1]
return(zip_state)
}
```
```{r}
#groups by diferent units
group_by_unit<-function(data, unit){
if(unit=="zipcode"){
countbyzip<-data%>%
group_by(zipcode) %>%
count() %>%
arrange(n)
return(countbyzip)
} else if (unit==="county") {
countbycounty<-data%>%
group_by(county) %>%
count() %>%
arrange(n)
return(countbycounty)
} else if (unit=="state"){
countbystate<-data%>%
group_by(state) %>%
count() %>%
arrange(n)
return(countbystate)
} else if (unit=="city"){
countbycity<-data%>%
group_by(city) %>%
count() %>%
arrange(n)
return(countbycity)
} else if (unit=="type"){
countbyprovider<-data%>%
group_by(Primary_Taxonomy) %>%
count() %>%
arrange(desc(n))
return(countbyprovider)
}
}
```
```{r}
countbyzip <- provider.data %>% #creating a count of practices by zip code
group_by(zipcode) %>%
count() %>%
arrange(n)
#countbyzip
#create a bar graph of providers by frequency
plot<-ggplot(countbyzip, aes(x=zipcode, y=n, fill="#FF6666"))+
geom_bar(stat="identity")
plot
#number of counties with 1 provider, 2 providers, etc
number_practices<-countbyzip %>%
select(n) %>% #selecting and grouping by frequency
group_by(n) %>%
count() %>%
arrange(n) #arranging by frequency
number_practices.low <- head(number_practices) #getting the first rows of the number_pratices and desingnating them as low numbers of practices
#add axis names
plot<-ggplot(number_practices.low, aes(x=factor(n), y=nn))+
geom_bar(stat="identity") + labs(x = "Number of providers", y = "Number of zip codes")
plot
```
```{r}
#Graphs of Provider Type
provider_grouping<-provider.data %>% #creating a count of practices by providers
group_by(Primary_Taxonomy) %>%
count() %>%
arrange(desc(n))
#provider_grouping
#general scatter of all providers types
provider_scatter<-ggplot(provider_grouping, aes(x=Primary_Taxonomy, y=n))+
geom_point()+
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank())+
labs(x = "Provider Types", y = "Number of providers")+
scale_color_brewer(palette="Set1")
provider_scatter
#create a bar graph of top 5 providers by frequency
top5providers<-head(provider_grouping)
providers_bar<-ggplot(top5providers, aes(x=Primary_Taxonomy, y=n))+
geom_bar(stat="identity")+
theme(axis.text.x = element_text(angle=90))+
labs(x = "Provider Type", y = "Number of Providers")
providers_bar
```
```{r}
NumProviderInZip<-function(zipcode,taxonomy){
url1<- "https://npiregistry.cms.hhs.gov/registry/search-results-table?addressType=ANY&postal_code=" #setting the url to scrape from
provider.data <- data.frame() #initializing an empty data frame
skips <- seq(0,9999999,100) #create skips
for (i in 1:length(zipcode)) { #iterating over all RI zip codes
for (j in 1:length(skips)){ #also iterating over skils
zip <- zipcode[i]
skip <- skips[j]
tax <- str_replace_all(taxonomy," ","+")
url<-paste0(url1,zip,"&skip=",skip,"&taxonomy_description=",tax) #pasting the url, with the rhode island zip code and including the skips
#text scrape to pull our places by zip code
h <- read_html(url, timeout = 200)
reps <- h %>% #setting up the repeating structure
html_node("table") %>%
html_table()
if (any(is.na(reps[,1])) | all(is.na(reps[,1]))) { #if the page has no physicians, move to next zip code
break}
reps$zip <- zipcode[i]
provider.data <- rbind(provider.data,reps) #binding together the provider data and reps data
}
}
colnames(provider.data) <- c("NPI","Name","NPI_Type","Primary_Practice_Address","Phone","Primary_Taxonomy","zipcode")
provider.data$street <- noquote( str_extract(provider.data$Primary_Practice_Address,pattern="^[a-zA-Z0-9_. -]+?(?=\n)")) #street name and number
provider.data$city <- noquote( str_extract(provider.data$Primary_Practice_Address,pattern="(?<=\t)[a-zA-Z_. -]+?(?=, )"))#city
provider.data$statename<- noquote( str_extract(provider.data$Primary_Practice_Address,pattern="(?<=, )[A-Z]+(?=\\s)")) #state postal code (2 characters)
provider.data<-mutate(provider.data, zipcode= as.character(zipcode))
zip_link<-read.csv("zcta_county_rel_10.txt") %>%
select(ZCTA5, STATE, COUNTY, GEOID) %>%
rename(zipcode = ZCTA5) %>%
mutate(zipcode = as.character(zipcode))
zip_link$zipcode = stri_pad_left(zip_link$zipcode, 5, "0")
NPI_join<-inner_join(provider.data, zip_link, by="zipcode")
census<-read.csv("co-est2017-alldata.csv")
NPI_to_census<-inner_join(NPI_join, census, by=c("STATE", "COUNTY"))
return(nrow(NPI_to_census))
}
```
```{r}
```