
Please save your answer to these questions as one .pdf le (use the save as
function in most word processors). Be sure to include your name, your teammate’s name
if there is anyone, and the assignment number. Submit the le to Carmen by the due
date.
More R and Dataset Management
Health and the Economy
How much does one’s personal situation aect their perspective on how their local economy is doing? There is plenty of actual evidence and common sense to suggest that often,
but not always, individuals accurately explain their local economy based solely on their
evaluation of personal and family situation. Some scholars argue that this eect is purely
articial and surveys are not good measures as respondents tend to use a `general feeling’
to mark everything as good or bad without dierentiation. Let’s prepare a dataset to address a potential case of this relationship. Follow the steps below, ultimately submitting
4 csv les along with answers and code for each item.
1. Download two specic state-level datasets from Gallup Analytics: Economic Con-
dence Index and Health Rating.
2. (.5 pt) Clean these two datasets (in Excel or a similar program) and save them as
csv les for submission using the steps below:
Delete the rst several rows so that the column names are the rst row in the
spreadsheet.
Delete the blank rst column.
Delete 10 rows of blank data after the numbers stop (it thinks there’s data
there)
Save and submit the datasets as gallup_econ.csv and gallup_health.csv, respectively.
3. Load the datasets into RStudio. Check that there are no extra blank rows of data
using the commands anyNA(econ$Time) and anyNA(health$Time) with your
named datasets (in place of `econ’ and `health’). The answer to both should be
`FALSE’.
1
4. Load the `reshape2′ and `plyr’ libraries.
5. (.5 pt) Using merge( ), combine the two datasets so that each row contains an
entry for exactly one state and one year. (456 obs)
6. (3 pt) Use ddply( ) to analyze the relationship between health status and economic
condence within each state. Set the constant variable to Geography and set the
function as summarize, specifying two dierent correlations (cor( )) separately
comparing economic condence to excellent health and to very good health. Use
summary( ) and report the minimum, mean, median, and maximum correlation for
each. Write two sentences about the discovered relationship between the variables.
7. (2 pt) Use dcast( ) to change the econ dataset from long to wide form, such that
each row is a state and each column is the economic condence for a year within the
range 2008 – 2016 (51 obs). Export this dataset as econ_cast.csv using write.csv(
) and submit with the assignment.
8. (2 pt) Use melt( ) to change the health dataset from wide to long form, such that
each row is the percentage of respondents from a particular state, in one year, for
a specic health status (2280 obs). Export this dataset as health_melt.csv using
write.csv( ) and submit with the assignment.
2