Hi, I am working with ACS data and I have used the dplyr package to filter my data to this. However, I am trying to use mutate within dplyr to find the increase and decrease in total population from 2013 to 2016 based on zip code. for example I want to create a new column that finds for zip 43001 what was the difference in total population from 2013 to 2016
My input:
filterdacs_D1 <- mutate(filename,difference1 =$zip$poulation[population]-$zip$population2017)
But this is incorrect as I want the difference between 2016 and 2014 for each zip code.
It's hard to help you with your sample data in this format, could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.
If you've never heard of a reprex before, you might want to start by reading this FAQ:
The answer from Big Ozzy and the responding comment from Andres at the Stack Overflow question are good.
Their approach is to:
take your data frame (Big Ozzy provided an example one called zips, but since you already have data, you can skip that and replace with your data frame's name, as Andres suggests.)
Filter to just include 2013 and 2016 numbers.
Spread so the 2013 population goes to a column called 2013 and the 2016 population goes to a column called 2016.
Big Ozzy's answer presumes there might be zips with fewer or more than one row of population for each year. (some might be new zips, or might accidentally have been included twice.) To deal with these, the answer sums all the 2013 and 2016 rows that might exist for that zip. In general, those should just be summing one number each.
The last step subtracts the difference between the two columns.
If this is not working for you, or not what you were looking for, it would be helpful to edit your question to explain.