forked from ajdamico/asdfree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1997-1999 mlces - download.R
100 lines (68 loc) · 2.99 KB
/
1997-1999 mlces - download.R
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
# analyze survey data for free (http://asdfree.com) with the r language
# medical large claims experience study
# 1997-1999
# # # # # # # # # # # # # # # # #
# # block of code to run this # #
# # # # # # # # # # # # # # # # #
# library(downloader)
# setwd( "C:/My Directory/MLCES/" )
# years.to.download <- 1997:1999
# source_url( "https://raw.githubusercontent.com/ajdamico/asdfree/master/Medical%20Large%20Claims%20Experience%20Study/1997-1999%20mlces%20-%20download.R" , prompt = FALSE , echo = TRUE )
# # # # # # # # # # # # # # #
# # end of auto-run block # #
# # # # # # # # # # # # # # #
# contact me directly for free help or for paid consulting work
# anthony joseph damico
# ajdamico@gmail.com
#########################################################################
# download the three years of the medical large claims experience study #
# with R, then save every file as an R data frame (.rda) #
#########################################################################
# set your working directory.
# all mlces files will be stored here
# after downloading and importing it.
# use forward slashes instead of back slashes
# uncomment this line by removing the `#` at the front..
# setwd( "C:/My Directory/MLCES/" )
# ..in order to set your current working directory
# remove the # in order to run this install.packages line only once
# install.packages( "downloader" , "digest" )
# uncomment this line to download all available data sets
# uncomment this line by removing the `#` at the front
# years.to.download <- 1997:1999
############################################
# no need to edit anything below this line #
# # # # # # # # #
# program start #
# # # # # # # # #
library(downloader) # downloads files easily from https sites
# create a temporary file and temporary directory
tf <- tempfile() ; td <- tempdir()
# loop through each of the years to download and..
for ( year in years.to.download ){
# determine the filepath on the society of actuaries' website to the file
fp <- paste0( "https://www.soa.org/Files/Research/" , year , ".zip" )
# download the current MLCES file
# and save it as the temporary file
download( fp , tf , mode = 'wb' )
# unzip all of the files in the downloaded .zip file into the current working directory
# then save all of their unzipped locations into a character vector called 'z'
z <- unzip( tf , exdir = getwd() )
# read the current file into RAM
x <- read.csv( z )
# convert fields to all lowercase
names( x ) <- tolower( names( x ) )
# save the mcles data table as an R data file (.rda)
# (for quick loading later)
save( x , file = paste0( "mcles" , year , ".rda" ) )
# delete the temporary file..
file.remove( tf )
# ..and the unzipped file
file.remove( z )
# remove `x` from memory and clear up RAM
rm( x )
# clear up RAM
gc()
}
# print a reminder: set the directory you just saved everything to as read-only!
message( paste( "all done. you should set" , getwd() , "read-only so you don't accidentally alter these files." ) )