-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_quickstart.Rmd
65 lines (51 loc) · 1.42 KB
/
db_quickstart.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
---
title: "Quick start to connect to the PE database"
author: "Chirag Patel"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(DBI)
library(dbplyr)
```
```{r}
path_to_dbname <- "./db/nhanes_012324.sqlite" ## change this to your path
## connect:
con <- DBI::dbConnect(RSQLite::SQLite(), dbname=path_to_dbname)
## list tables:
DBI::dbListTables(con)
```
```{r}
## all the variable names:
var_names <- tbl(con, "variable_names_epcf")
## all the table names (aka Data.File.Name)
table_names <- tbl(con, "table_names_epcf")
table_names
```
```{r}
## get sample table(s) for spirometry from the NHANES 2009-2010 (F)
demo_f <- tbl(con, "DEMO_F")
spx_f <- tbl(con, "SPX_F")
spx_demo <- demo_f |> left_join(spx_f, by="SEQN")
spx_demo
spx_demo |> group_by(RIAGENDR) |> summarize(mean_fev1 = mean(SPXNFEV1))
p <- ggplot(spx_demo, aes(RIDAGEYR, SPXNFEV1, color=factor(RIAGENDR)))
p <- p + geom_point() + xlab("Age") + ylab("N_FEV1")
p
```
```{r mortality}
mortality <- tbl(con, "mortality")
## merge with demo from 1999-2000
mortality <- mortality |> left_join(tbl(con, "DEMO"), by="SEQN")
mortality <- mortality |> mutate(death_age = RIDAGEYR + permth_exm/12)
p <- ggplot(mortality |> filter(eligstat == 1), aes(RIDAGEYR, death_age, color=factor(RIAGENDR)))
p <- p + geom_point() + xlab("Age") + ylab("Age at Death")
p
```
```{r}
dbDisconnect(con)
```