Thanks in advance for any help or input.
I am getting this error: "Error in abs(x) : non-numeric argument to mathematical function" when I try to plot the edited data frame.
Here's what I'm trying to do, I have no idea where the error is coming from:
- Separate age increments into separate columns
- Get rid of all the final entries with a + (or ideally change them all to ... to 100)
- Calculate the range of each increment
- Plot the various increments in a horizontal stacked bar plot
This is the code.:
require(tidyr)
require(dplyr)
require(ggplot2)
CoRo_age <- CoRo_age %>%
separate(age_cat, c("Increment 1", "Increment 2", "Increment 3", "Increment 4", "Increment 5",
"Increment 6", "Increment 7", "Increment 8", "Increment 9", "Increment 10", "Increment 11",
"Increment 12","Increment 13", "Increment 14", "Increment 15", "Increment 16","Increment 17"), ",")
CoRo_age$'Increment 2'[!grepl("-",CoRo_age$'Increment 2')] <- ""
CoRo_age$'Increment 3'[!grepl("-",CoRo_age$'Increment 3')] <- ""
CoRo_age$'Increment 4'[!grepl("-",CoRo_age$'Increment 4')] <- ""
CoRo_age$'Increment 5'[!grepl("-",CoRo_age$'Increment 5')] <- ""
CoRo_age$'Increment 6'[!grepl("-",CoRo_age$'Increment 6')] <- ""
CoRo_age$'Increment 7'[!grepl("-",CoRo_age$'Increment 7')] <- ""
CoRo_age$'Increment 8'[!grepl("-",CoRo_age$'Increment 8')] <- ""
CoRo_age$'Increment 9'[!grepl("-",CoRo_age$'Increment 9')] <- ""
CoRo_age$'Increment 10'[!grepl("-",CoRo_age$'Increment 10')] <- ""
CoRo_age$'Increment 11'[!grepl("-",CoRo_age$'Increment 11')] <- ""
CoRo_age$'Increment 12'[!grepl("-",CoRo_age$'Increment 12')] <- ""
CoRo_age$'Increment 13'[!grepl("-",CoRo_age$'Increment 13')] <- ""
CoRo_age$'Increment 14'[!grepl("-",CoRo_age$'Increment 14')] <- ""
CoRo_age$'Increment 15'[!grepl("-",CoRo_age$'Increment 15')] <- ""
CoRo_age$'Increment 16'[!grepl("-",CoRo_age$'Increment 16')] <- ""
CoRo_age$'Increment 17'[!grepl("-",CoRo_age$'Increment 17')] <- ""
CoRo_diff <- apply(CoRo_age[1:50,2:17], c(1,2), function(x) abs(as.numeric(unlist(eval(parse(text=x))))))
CoRo_age <- as.data.frame(cbind(CoRo_age[,1], CoRo_diff))
colnames(CoRo_age)[1] <- c("States")
CoRo_age %>%
gather("Increments", "Range", -States) %>%
ggplot(aes(x=States, y=Range)) +
geom_bar(stat="identity", fill=Increments) +
coord_flip()
This is the dput output for the data frame:
CoRo_age <- structure(list(state = c("Minnesota", "New York", "North Carolina",
"South Carolina", "Delaware", "Illinois", "Louisiana", "Georgia",
"Arkansas", "Connecticut", "Idaho", "Maryland", "Massachusetts",
"Tennessee", "Michigan", "California", "Virginia", "District of Columbia",
"Kentucky", "Arizona", "Maine", "Colorado", "Hawaii", "Indiana",
"Iowa", "Kansas", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "Texas",
"Rhode Island", "South Dakota", "Alabama", "Alaska", "Oklahoma",
"Oregon", "Pennsylvania", "Utah", "Wisconsin", "West Virginia",
"Vermont", "Wyoming", "Washington", "North Dakota", "Florida"
), age_cat = c("0-5,6-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+", "0-17,18-24,25-49,50-64,65-74,75+",
"11-20,21-30,31-40,41-50,51-60,61-70,71-80,81+", "0-4,5-17,18-34,35-49,50-64,65+",
"0-20,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-5,6-17,18-29,30-39,40-49,50-59,60-69,70+",
"0-1,01-04,05-09,10-17,18-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-17,18-24,25-44,45-64,65+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-18,18-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-10,11-20,21-30,41-50,51-60,61-70,71-80,80+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-17,18-49,50-64,65+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-4,5-14,15-19,20-24,25-34,35-44,45-54,55-64,65-74,75+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-20,20-44,45-54,55-64,65,+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-39,40-59,60+",
"0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-17,18-40,41-60,61-80,80+",
"0-9,10-17,18-24,25-34,35-44,45-54,55-64,65-74,75-84,85+", "0-18,18-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-9,10-19,20-24,25-29,30-34,35-39,40-44,45-49,50-54,55-59,60-64,65-69,70-74,75-79,80-84,85-90,90+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-34,35-44,45-54,55-64,65-74,75-84,85+",
"0-10,10-19,20-29,30-39,40-49,50-59,60-69,70+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-4,5-17,18-29,30-49,50-64,65-79,80+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-1,1-9,10-19,20-29,30-39,40-49,50-59,60-64,65-69,70-74,75-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-24,25-49,50-64,65+,",
"0-10,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-17,18-35,36-49,50-64,65+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,>100",
"1-14,15-24,25-44,45-64,65-84,85+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-18,19-29,30-39,40-49,50-59,60-69,70-79,80+", "00-19,20-39,40-59,60-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-14,15-24,25-34,35-44,45-54,55-64,65-74,75-84,85+"
)), row.names = c(1L, 3L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L,
21L, 24L, 25L, 28L, 36L, 39L, 40L, 41L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 75L, 76L, 81L, 82L, 83L, 86L, 87L, 88L, 97L, 98L, 101L,
104L, 126L), class = "data.frame")