worksheet-4.R 1.5 KB
Newer Older
Zheng Liu's avatar
Zheng Liu committed
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
## Tidy Concept

trial <- read.delim(sep = ',', header = TRUE, text = "
block, drug, control, placebo
    1, 0.22,    0.58,    0.31
    2, 0.12,    0.98,    0.47
    3, 0.42,    0.19,    0.40
")

## Gather 

library(tidyr)
tidy_trial <- ...(trial,
  key = ...,
  value = ...,
  ...)

## Spread 

survey <- read.delim(sep = ',', header = TRUE, text = "
participant,   attr, val
1          ,    age,  24
2          ,    age,  57
3          ,    age,  13
1          , income,  30
2          , income,  60
")

tidy_survey <- ...(survey,
  key = ...,
  value = ...)

tidy_survey <- spread(survey,
  key = attr,
  value = val,
  ...)

## Sample Data 

library(data.table)
cbp <- fread('data/cbp15co.csv')

cbp <- fread(
  'data/cbp15co.csv',
  ...,
  ...)

acs <- fread(
  'data/ACS/sector_ACS_15_5YR_S2413.csv',
  colClasses = c(FIPS = 'character'))

## dplyr Functions 

library(...)
cbp2 <- filter(...,
  ...,
  !grepl('------', NAICS))

library(...)
cbp2 <- filter(cbp,
  ...)

cbp3 <- mutate(...,
  ...)

cbp3 <- mutate(cbp2,
  FIPS = str_c(FIPSTATE, FIPSCTY),
  ...)

...
  filter(
    str_detect(NAICS, '[0-9]{2}----')
  ) ...
  mutate(
    FIPS = str_c(FIPSTATE, FIPSCTY),
    NAICS = str_remove(NAICS, '-+')
  )

...
  ...(
    FIPS,
    NAICS,
    starts_with('N')
  )

## Join

sector <- fread(
  'data/ACS/sector_naics.csv',
  colClasses = c(NAICS = 'character'))

cbp <- cbp %>%
  ...

## Group By 

cbp_grouped <- cbp %>%
  ...

## Summarize 

cbp <- cbp %>%
  group_by(FIPS, Sector) %>%
  ...
  ...

acs_cbp <- ... %>%
  ...