I am trying to create a new column on a merged dataframe for the per capita value of variable p in different states. Then I am trying to put them onto a ggspatial map. Below is my code.
data(us_states)
#Merge
st_geometry(state_p) <- NULL #bc R can't merge points and polygon
p_states <- merge(us_states, state_p, by.x = "NAME", by.y = "Name", all=T)
view(p_states)
# This all works fine
p_states %>%
summarize(p_capita = (state_n/total_pop_15)) %>%
print(p_states$p_capita)
#This prints in the console but not in a dataframe, more like in [this way which confuses me][1]
view(p_states)
#p_capita won't show up here
plot(st_geometry(us_states))
plot(p_states["p_capita"])
#this plot will not run
#the error message is "Error in `[.data.frame`(x, i) : undefined columns selected"
[1]: https://i.stack.imgur.com/wIM9B.png
I suspect it has something to do with the fact it's spatial data? This is my first time working with ggspatial so I'm out of my element.