Skip to content

Instantly share code, notes, and snippets.

@EmilHvitfeldt
Created January 31, 2018 17:37
Show Gist options
  • Save EmilHvitfeldt/9bf6d6e085254719e9db5aaa19bc5bc5 to your computer and use it in GitHub Desktop.
Save EmilHvitfeldt/9bf6d6e085254719e9db5aaa19bc5bc5 to your computer and use it in GitHub Desktop.
data points in states
library(maps)
library(tidyverse)
# Having the data.csv file in the same working directory
data <- read_csv("data.csv")
data_states <- data %>%
mutate(state = map.where('state', Longitude, Latitude)) %>%
# Removes point not in a state
filter(!is.na(state)) %>%
# Some states are subdivided, this mutate call collapes them together
mutate(state = if_else(str_detect(state, ":"),
str_extract(state, ".+?(?=:)"),
state))
data_states %>%
count(state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment