-
Notifications
You must be signed in to change notification settings - Fork 7
/
unc_hw4.Rmd
197 lines (147 loc) · 4.88 KB
/
unc_hw4.Rmd
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
---
title: "R Notebook - Module 1 Homework 4 - R+SQL"
output: html_document
---
Go to this site and Download the database:
<https://github.com/coreysparks/r_courses/blob/master/data/PUMS_data>
![](C:/Users/ozd504/Github/r_courses/github_db.png)
and save it to your local computer. I recommend putting the file *PUMS_data* in your User folder on your computer.
The two packages we need here are `DBI` and `RSQLite` to connect to this database.
```{r}
library(tidyverse)
library(DBI)
library(RSQLite)
```
```{r}
con <-dbConnect(drv = SQLite(),
dbname = "data/PUMS_data")
con
```
We can list all of the tables in the database by using `dbListTables()`
```{r}
dbListTables(con)
```
The table `pums_tx_ca` is what you used in homework 1 and 2, while the `pums_tx_ca_hh` is different data on the households that each of the people in the `pums_tx_ca` live in.
### Using SQL queries in R
One way to perform a SQL query is to write the query as a text string, then submit it to `dbGetQuery()`
:
```{r}
myquery<- 'select AGEP, SEX, ST_LABEL from pums_tx_ca limit 10;'
```
You can store the data from the query in a R object:
```{r}
mydata <- dbGetQuery(conn = con, statement = myquery)
mydata
```
Or we can put the query directly into the `dbGetQuery()` function:
```{r}
mydata<- dbGetQuery(conn = con,
statement = 'select AGEP, SEX, ST_LABEL
from pums_tx_ca limit 10;')
mydata
```
*Same result*
### Select columns from table
```{r}
query<-"
SELECT AGEP, SEX, ST, WAGP
FROM pums_tx_ca limit 10;
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
### Filter cases
Select cases of adults over age 25 with a doctorate and who earned a wage.
```{r}
query<-"
SELECT AGEP, SEX, ST, WAGP, SCHL
FROM pums_tx_ca
WHERE AGEP >=25 AND SCHL = 24 AND WAGP > 0
limit 10;
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
### Aggregation
Find the average wage for PhD's in Texas and California
```{r}
query<-"
SELECT ST_LABEL, AVG(WAGP)
FROM pums_tx_ca
WHERE AGEP >=25 AND SCHL = 24 AND WAGP > 0
GROUP BY ST
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
Find the average wage for PhD's in Texas and California by Gender
```{r}
query<-"
SELECT ST_LABEL, SEX_LABEL, AVG(WAGP)
FROM pums_tx_ca
WHERE AGEP >=25 AND SCHL = 24 AND WAGP > 0
GROUP BY ST, SEX
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
### Left Joins
We can join data from the household table to the individual table using a left join. The two tables have the `SERIALNO` field in common.
To select fields from both tables, use the syntax `table1.column`, `table2.column`
Here, I select a few coluns from the person table (`pums_tx_ca`) and the home value column `VALP` from the housing table `pums_tx_ca_hh`, and join them.
I also select cases like above for people over age 25 with a PhD.
```{r}
query<-"
SELECT
pums_tx_ca.SERIALNO, pums_tx_ca.AGEP, pums_tx_ca.SCHL, pums_tx_ca.ST, pums_tx_ca.SEX, pums_tx_ca.WAGP , pums_tx_ca_hh.TEN,
pums_tx_ca_hh.SERIALNO, pums_tx_ca_hh.VALP
FROM pums_tx_ca
LEFT JOIN pums_tx_ca_hh ON pums_tx_ca.SERIALNO = pums_tx_ca_hh.SERIALNO
WHERE pums_tx_ca.AGEP >=25 AND pums_tx_ca.SCHL = 24 AND pums_tx_ca.WAGP > 0 AND pums_tx_ca_hh.VALP>0
LIMIT 10;
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
### Aggregate from joined tables
Here I calculate the average home value in Texas and California for those persons with a PhD
```{r}
query<-"
SELECT
pums_tx_ca.SERIALNO, pums_tx_ca.ST_LABEL,
AVG(pums_tx_ca_hh.VALP) AS mean_home_value
FROM pums_tx_ca
LEFT JOIN pums_tx_ca_hh ON pums_tx_ca.SERIALNO = pums_tx_ca_hh.SERIALNO
WHERE pums_tx_ca.AGEP >=25 AND pums_tx_ca.SCHL = 24 AND pums_tx_ca.WAGP > 0 AND pums_tx_ca_hh.VALP>0
GROUP BY pums_tx_ca.ST_LABEL
LIMIT 10;
"
mydata<- dbGetQuery(conn = con,
statement = query)
mydata
```
### Creat a merged table for further analysis
```{r}
query<-"
SELECT
pums_tx_ca.SERIALNO, pums_tx_ca.AGEP, pums_tx_ca.SCHL, pums_tx_ca.ST_LABEL, pums_tx_ca.SEX, pums_tx_ca.WAGP ,
pums_tx_ca_hh.SERIALNO, pums_tx_ca_hh.VALP
FROM pums_tx_ca
LEFT JOIN pums_tx_ca_hh ON pums_tx_ca.SERIALNO = pums_tx_ca_hh.SERIALNO
WHERE pums_tx_ca.AGEP >=25 AND pums_tx_ca.SCHL = 24 AND pums_tx_ca.WAGP > 0 AND pums_tx_ca_hh.VALP>0
"
mydata<- dbGetQuery(conn = con,
statement = query)
head(mydata)
dim(mydata)
```
### <span style="color:red">Homework Assignment</span>
Using the ACS tables from above perform the following
1) How many households in Texas had values over $500,000?
2) Create a table of persons in California with less than a High School degree. How many people are in this table?
3) Create a merged table with the columns ST_LABEL, AGEP, RAC1P, WAGP, ACCESS and TENURE for persons in California with less than a bachelor's degree.