New R user looking to assign the FIPS code to the counties within a dataset. I have multiple point data with a county name attached to the information, and I want to assign the appropriate FIPS code to all counties within the dataset.
Data example:
State StateFIPS County
DE 10 Sussex
DE 10 Sussex
DE 10 Kent
DE 10 Sussex
I have been able to attach the FIPS by state using:
DEdata$StateFIPS <- fips(DEdata$State)
But I am unable to use this fips function for the County level. Is there anyway to assign the FIPS code for multiple counties within the state?
For anybody interested, I was able to download the FIPS codes using the TIGRIS package, and then assign it with various join functions.
##Assign FIPS by State
##DE_2016small$StateFIPS <- fips(DE_2016small$Residence_Addresses_State_2016)
##Assign FIPS by County
##Download FIPS from TidyCensus/Tigris and filter DE (Using TIGRIS) and filter
DE_counties <- counties("DE")
DE_counties <- DE_counties %>%
select(STATEFP, COUNTYFP, NAME)
DE_counties$geometry <- NULL
##Merge to match the County with its FIPS
DEwFIPS <- merge(DE_counties, DE_2016county, by.x = "NAME", by.y = "County", all = TRUE)
##Concatenate FIPS codes together
DEwFIPS$GEOID <- str_c(DE_FIPSbroad$STATEFP, DE_FIPSbroad$COUNTYFP, DE_FIPSbroad$Residence_Addresses_CensusTract_2016)
##Tidy data
DEwFIPS <- DEwFIPS %>% relocate(GEOID, .before = NAME)````
Related
I am working with ISO-3 names of the countries. Below you can see one example.I want to use this data for Choropleth map.
datar <- data.frame(GEO = c("GR","AZ","TR","GA","IR"),
Value = c(0.5560,0.323,0.2140,0.312,0.0225),
search_countries = factor(c("Greece","Azerbaijan","Turkey","Georgia","Iran")),
countries = c("Greece","Azerbaijan","Turkey","Georgia","Iran")
)
map <- map_data('world', region = datar$search_countries)
The code's last line allows me to draw a Choropleth map. Now I want to have an additional column with the continent names for each country next to the column subregion.
So can anybody help me how to solve this problem and to have a continent name for each country on the table?
If I understand correctly you want to add a column containing the relevant continents for each country in your dataframe?
Here we can get the country and continent names using the raster package then join them onto your dataframe :)
country.codes <- raster::ccodes()
map.continents <- left_join(map, country.codes, by = c('region' = 'NAME')) %>%
select(all_of(colnames(map)), continent)
I am trying to make a graph showing the average temp in Australia from 1950 to 2000. My dataset contains a "Country" table which contains Australia but also other countries as well. The dataset also includes years and average temp for every country. How would I go about excluding all the other data to make a graph just for Australia?
Example of the dataset
You just need to subset your data so that it only contains observations about Australia. I can't see the details of your dataset from your picture, but let's assume that your dataset is called d and the column of d detailing which country that observation is about is called country. Then you could do the following using base r:
d_aus <- d[d$country == "Australia", ]
Or using dplyr you could do:
library(dplyr)
d_aus <- d %>%
filter(country == "Australia")
Then d_aus would be the dataset containing only the observations about Australia (in which `d$country == "Australia"), which you could use to make your graph.
This should make the job. Alternatively, change the names of the columns to those of yours.
library("ggplot2")
library("dplyr")
data %>% filter(Country == "Australia" & Year %in% (1950:2000)) %>% ggplot(.,aes(x=Year,y=Temp)) + geom_point()
I currently have a dataframe of pairs of country codes (like US, RU, CA etc.) Is there a function that determines the centroid of a country given the country code so that I can find the distance between the pairs of countries? Or is there a function that can give me the coordinates of the centroid of each country (such as the longitude and latitude for example)?
This is the first couple lines of my dataset that I had filtered from a previous one for reference.
You can scrape this google public dataset.
My previous suggestion to use the countryref dataset in package CoordinateCleaner doesn't work because I found out there are duplicates with different positions.
library(rvest)
library(dplyr)
url <- 'https://developers.google.com/public-data/docs/canonical/countries_csv'
webpage <- read_html(url)
centroids <- url %>% read_html %>% html_nodes('table') %>% html_table() %>% as.data.frame
data <- data.frame(V1 = c("US","US"), V2 = c('VN','ZA'))
data %>% inner_join(centroids,by = c("V1"="country")) %>% inner_join(centroids,by = c("V2"="country"))
V1 V2 latitude.x longitude.x name.x latitude.y longitude.y name.y
1 US VN 37.09024 -95.71289 United States 14.05832 108.27720 Vietnam
2 US ZA 37.09024 -95.71289 United States -30.55948 22.93751 South Africa
I'm a noob in r programming.
I have 2010 census data in the link-
census data.
This is my dataframe-
dataframe.
What I'd like to do is add the population column 'P001001' from the census data for each state into the dataframe. I'm not able to figure out how to map the state abbreviations in the dataframe to the full names in the census data, and add the respective population to each row for that state in the data frame. The data is for all of the states. What should be the simplest way to do this?
Thanks in advance.
Use the inbuilt datasets for USA states: state.abb and state.name see State name to abbreviation in R
Here's a simple bit of code which will give you a tidyverse approach to the problem.
1) add the state abbreviation to the census table
2) left join the census with the df by state abbrevation
library(tibble)
library(dplyr)
census <-tibble(name = c("Colorado", "Alaska"),
poo1oo1 = c(100000, 200000))
census <-
census %>%
mutate(state_abb = state.abb[match(name, state.name)])
df <- tibble(date = c("2011-01-01", "2011-02-01"),
state = rep("CO", 2),
avg = c(123, 1234))
df <-
df %>%
left_join(census, by = c("state" = "state_abb"))
I'm making a Leaflet map in R and I want to assign certain data to the specific areas. Right now I'm using a placeholder just to try and figure it out but I'm not having luck. I have certain data from an excel file that I want to assign to certain counties. How would I go about that?
library(maptools)
library(leaflet)
library(rjson)
library(magrittr)
library(sf)
library(xlsx)
## reads in the JSON data of all counties in USA
counties <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json")
## selects kansas and missouri county lines
kscounties<-counties[counties$STATE=="20",]
mocounties<-counties[counties$STATE=="29",]
## variable containing all kansas and missouricounty names
kscountynames<-kscounties$NAME
mocountynames<-mocounties$NAME
## combines both counties
bothcounties<-rbind(kscounties,mocounties)
bothcountynames<-c(kscountynames,mocountynames)
## color pallette
pal<-colorNumeric("viridis",NULL)
## placeholder
percent=c(1:100)
## creates leaflet of kansas and missouri counties
leaflet(bothcounties) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
fillColor = ~pal(percent),
label = ~paste(bothcountynames, "\n", formatC(percent, big.mark = ",")
)) %>%
setView(-98.4,38.5,zoom=6) %>%
addLegend(position="bottomright",pal = pal, values = percent, opacity = 1.0,title="Percent") %>%
addLayersControl(position="topleft",
baseGroups = c("1","2"),
overlayGroups=c("A","B","C","D","E","F")
)
You can just create a separate dataframe containing the data in question and refer to it when plotting. You just have to be careful to ensure the order of counties in the dataframe matches the order of counties in the geo-data. The county names you're already extracting should work as a 'key' to match that order.
From your code above, change the #placeholder part to..
data_to_plot <- data.frame("NAME"=bothcountynames,"data"=###YOURDATA_IN_CORRECT_ORDER###))
..with the data you want to plot. You could also just set-up a single-column dataframe with the names and then do a merge/join as it may be an easier way to maintain the required order.
In the leaflet call, put fillColor = pal(data_to_plot$data). The ~ basically won't be necessary if the data you're referencing is stored in a separate object.