I created this code and when I ran it, it only showed one observation in my line graph. I am not getting any error messages. Can someone let me know how to fix the loop and I received this error message: Ideally there should be a line for each observation/drug type.
plot(0, 0, type="n", main="Drug Related Emergency Department Visits", xlab="", ylab="", xlim=c(2004,2011), ylim=c(1000,800000))
for (i in 1:length(ER$Drugs)) { vals <- ER[i,]
lines(c(2004, 2011), c(vals$Estimates.2004, vals$Estimates.2011))
}
My data is as follows:
structure(list(Drugs = structure(c(1L, 4L, 5L, 8L), .Label = c("Alcohol-alone",
"Amphetamines", "Antidepressants", "Cocaine", "Heroin", "Inhalants",
"LSD", "Marijuana", "Methamphetamine", "Opiates/opioids", "PCP",
"Stimulants"), class = "factor"), Estimates.2004 = structure(c(2L,
10L, 6L, 7L), .Label = c("132,576", "150,988", "162,435", "198,126",
"2,146", "214,432", "281,619", "31,342", "34,085", "475,425",
"81,889", "9,525"), class = "factor"), Estimates.2011 = structure(c(4L,
9L, 6L, 8L), .Label = c("10,032", "102,961", "108,388", "117,653",
"159,840", "258,482", "4,819", "455,668", "505,224", "556,551",
"70,831", "75,538"), class = "factor")), .Names = c("Drugs",
"Estimates.2004", "Estimates.2011"), row.names = c(NA, 4L), class = "data.frame")