Skip to content

Commit

Permalink
resolve conflict Merge branch 'main' of https://github.com/RSGInc/tra…
Browse files Browse the repository at this point in the history
  • Loading branch information
erika-redding committed Jan 31, 2024
2 parents b5e7b63 + 64cab9d commit 7de27b7
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Suggests:
knitr (>= 1.45),
rmarkdown (>= 2.25),
ggplot2 (>= 3.4.4),
bit64
bit64,
tigris
Config/testthat/edition: 3
Depends:
R (>= 2.10)
Expand Down
19 changes: 19 additions & 0 deletions data-raw/create_test_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ ids_to_keep = sample(hts_data$hh$hh_id, size = 1000)

hts_data = lapply(hts_data, function(dt) dt[hh_id %in% ids_to_keep])

# Create fake home counties and lat/lng for geographic analysis
hts_data$hh[, home_county := sample(1:3, size = nrow(hts_data$hh), replace = TRUE)]

hts_data$hh[, home_lat := sample(seq(from = 33.00000, to = 40.00000,
length.out = nrow(hts_data$hh)),
size = nrow(hts_data$hh), replace = TRUE)]

hts_data$hh[, home_lon := sample(seq(from = -100.00000, to =-80.00000,
length.out = nrow(hts_data$hh)),
size = nrow(hts_data$hh), replace = TRUE)]

## Choose a subset of columns --------------------------------------------------
keep_cols = c(
Expand All @@ -41,6 +51,9 @@ keep_cols = c(
'income_followup',
'num_people',
'residence_type',
'home_county',
'home_lat',
'home_lon',

# Person variables:
'race_1',
Expand Down Expand Up @@ -125,7 +138,13 @@ setDT(value_labels)
variable_list = variable_list[variable %in% keep_cols]
value_labels = value_labels[variable %in% keep_cols]

# add fake home_county labels to value_labels and remove real ones
value_labels = value_labels[variable != 'home_county']

county_labels = data.frame(variable = rep('home_county', 3), value = 1:3,
label = c('Arike County', 'Clark County', 'Moore County'))

value_labels = rbind(value_labels, county_labels, fill = TRUE)
## Subset to minimum required columns ------------------------------------------
variable_list = variable_list[, c(
'variable',
Expand Down
Binary file modified data/day.rda
Binary file not shown.
Binary file modified data/hh.rda
Binary file not shown.
Binary file modified data/person.rda
Binary file not shown.
Binary file modified data/test_data.rda
Binary file not shown.
Binary file modified data/trip.rda
Binary file not shown.
Binary file modified data/value_labels.rda
Binary file not shown.
Binary file modified data/variable_list.rda
Binary file not shown.
Binary file modified data/vehicle.rda
Binary file not shown.
50 changes: 50 additions & 0 deletions vignettes/geographic_summaries.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "Creating user-specified geographic summaries"
description: >
output: html_vignette
vignette: >
%\VignetteIndexEntry{Creating user-specified geographic summaries}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
library(travelSurveyTools)
library(data.table)
library(tigris)
```

### Creating summaries by geographic variables using hts_summary

`hts_summary` can summarize geographic variables.

```{r, county_income, echo=TRUE, eval=TRUE}
DT = hts_prep_data(summarize_var = 'income_detailed', summarize_by = 'home_county', data = test_data)
output = hts_summary(prepped_dt = DT$cat, summarize_var = 'income_detailed', summarize_by = 'home_county')
factorize_df(output$summary$unwtd, vals_df = value_labels, value_label_colname = 'label')
```

### Using spatial_join to join together separate shapefiles

`spatial_join` can link together multiple geographies.

```{r, spatial_join, echo=TRUE, eval=TRUE}
states = states()
hh = join_spatial(
hh,
dplyr::select(states, home_state = NAME),
id_col = 'hh_id',
lon_col = 'home_lon',
lat_col = 'home_lat',
largest = TRUE)
hh[, .N, home_state]
```

0 comments on commit 7de27b7

Please sign in to comment.