Skip to content

Instantly share code, notes, and snippets.

@ajdamico
Created September 19, 2019 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdamico/629cc9d86c86d139dfba015b6b18f81d to your computer and use it in GitHub Desktop.
Save ajdamico/629cc9d86c86d139dfba015b6b18f81d to your computer and use it in GitHub Desktop.
tf <- tempfile()
download.file( "https://www.ssa.gov/oact/babynames/state/namesbystate.zip" , tf , mode = 'wb' )
z <- unzip( tf , exdir = tempdir() )
state_files <- grep( "\\.TXT$" , z , value = TRUE )
state_list <- lapply( state_files , read.table , header = FALSE , sep = ',' , stringsAsFactors = FALSE )
w <- do.call(rbind,state_list)
names( w ) <- c( 'stateab' , 'sex' , 'year' , 'name' , 'count' )
maxes <- with( w , tapply( count , list( year , stateab , sex ) , max ) )
max_df <- reshape2::melt( maxes )
names( max_df ) <- c( 'year' , 'stateab' , 'sex' , 'count' )
all_maxes <- merge( w , max_df )
sort( table( all_maxes[ , 'name' ] ) )
# mostly nevada
subset( all_maxes , name == 'Anthony' )
# jacob threatens
merge( all_maxes , subset( all_maxes , name == 'Anthony' )[c('stateab','sex','year','count')] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment