R Club 10.20.15

In today’s R Club session we’ll split the time between reviewing Basic R and a consultation.

In the Dropbox link below there are two sets of R Practice Code (called rclub1 and rclub2 and associated data files). The code typically has instructions and a function, but you’ll need to fill in the arguments. The plan is to give everyone some time to try it out on their own and then review and/or answer any questions. We’ll do either one or both of the sets (depending on how much time it takes) because we want to make sure to get to our first consultation of the year!

Here is what Rita would like help with:

She’d like to: a) pull zipcodes and scale scores from my excel data into R, b) match these zipcodes to a map of the US with state borders [I don’t care about zipcode borders], c) turn the scale + zipcode data into a heatmap of the country.

If you’re new to R Club, typically during a consultation we all try to work on the problem together and at the end of the session we’ll review what we figured out (or create a post about it if we run out of time). Rita’s example data are also in the Dropbox link below.

Dropbox Link:

https://www.dropbox.com/sh/wuk6els34nfb62k/AACMVHxAhy5BhM9ahj-BIeS6a?dl=0

Here are some sources that I’ve found to start thinking about how to create the sort of heat maps that Rita would like to create in R:

http://blog.revolutionanalytics.com/2014/01/easy-data-maps-with-r-the-choroplethr-package-.html

http://www.trulia.com/blog/tech/the-choroplethr-package-for-r/

Edited to add another potentially helpful link:

https://cran.r-project.org/doc/contrib/intro-spatial-rl.pdf

One comment

  1. rosem@uoregon.edu

    if(!require(zipcode)) install.packages(“zipcode”); library(zipcode)
    df.raw <- read.csv("StudyFiveSampleOneWaveOne_forRclub.csv")
    if(!identical(x=clean.zipcodes(df.raw$ZipCode), y=as.character(df.raw$ZipCode)) ) message("Uh-oh") #check that zip codes in df are clean
    df <- df.raw
    df$zip <- clean.zipcodes(df$ZipCode)
    df <- na.omit(df)

    data(zipcode) # reads in a dataframe with the state for each zipcode
    df <- merge(df, zipcode[,c(1,3)], by="zip", all.x=T) # add a column of state abbreviations to the datafile

    library(dplyr)
    df.state %
    group_by(state) %>%
    summarize(PScore=mean(PScore))

    library(choroplethrMaps)
    data(state.regions) # reads in a dataframe with the state name and state abbreviations

    df.state <- merge(df.state, state.regions[,c(1,2)], by.x="state", by.y="abb", all.x=T)

    map.data = data.frame(region=df.state$region, value=df.state$PScore)
    state_choropleth(map.data)

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>