Adding values from a dataframe to a different dataframe - r

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"))

Related

Continent names and choropleth maps

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)

Making a graph from a dataset

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()

How to assign the FIPS codes to multiple Counties by name?

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)````

r - ratio calculation via data set

df <- read.csv('https://raw.githubusercontent.com/ulklc/covid19-
timeseries/master/countryReport/raw/rawReport.csv')
df$countryName = as.character(df$countryName)
I processed the dataset.
df$countryName[df$countryName == "United States"] <- "United States of America"
Changed here for United States of America Arrived in population data.
df8$death_pop <- df8$death / df8$PopTotal
I totally calculated the death/pop.
most, 10 countries. death/pop. how can I find?
Using base R:
df8[order(df8$death_pop, decreasing = TRUE)[1:10],]
This orders your data.frame by death_pop and extracts the first 10 rows.
Using the package dplyr there is the function top_n, which gives you the desired result. I added arrange(desc() to give you a sorted output. Remove this part if you don't need it.
df8 %>% top_n(10, death_pop) %>% arrange(desc(death_pop))

Distance Matrix in R using geosphere

I have a dataset with info on international investments in Europe and coordinates about NUTS3. For each investment I have the city and the coordinates (lat1,long1). I want to compute the distance from each city to each of the NUTS 3 I have --> E.G. Paris to Paris, Paris_Lyone, Paris_Orly, Paris_Maidenhead etc etc. I want to loop this mechanism for all the cities I have, so at the end I have a matrix for each city that include its distance to each NUTS. I tried to use geosphere but it gives me just the distance between rows.
summary(coordinate$NUTS_BN_ID)
summary(fdimkt$NUTS_BN_ID)
##merge dataset
df <- merge(fdimkt,coordinate, by="nutscode", all = FALSE)
View(df)
fix(df)
#install.packages("dplyr")
library(dplyr)
df %>% dplyr::rename(lat1= `_destination_latitude`, long1= `_destination_longitude` )
library(geosphere)
library(data.table)
#dt <- expand.grid.df(df,df)
setDT(df)[ , dist_km := distGeo(matrix(c(`_destination_latitude`, `_destination_longitude`), ncol = 2),
matrix(c(`lat2`, `long2`), ncol = 2))/1000]
summary(df$dist_km)
This didn't work because it returns me the distance by row, but I actually want the distance from each city to all the NUTS3 coordinates I have
Someone can help me with this?
I'm not sure on how to post my dt, this I gues that might help to have more suggestions.

Resources