0

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")
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
  • Please consider adding a newline and four spaces before each line of code. You can also select the code and use `ctrl + k` shortcut. you can also quote your code in backticks '`' if you want to have inline code. – yukashima huksay Jan 14 '18 at 06:34
  • 2
    Please post sample data that can be copied; there's no way I'm spending time trying to transcribe an image of data like that. Some good references: https://stackoverflow.com/help/mcve and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – r2evans Jan 14 '18 at 06:35
  • Have you tried this with a single call to `lines` instead of within a `for` loop? If there are any error messages, please include the command and the associated error message (verbatim). – r2evans Jan 14 '18 at 06:41

0 Answers0