I'm trying to loop through a list which is used to subset a data.table and run the results through few lines of code. Subseting with a variable is working perfectly without the for loop but when the loop is included it does not work as expected. I guess the problem is either caused by data type or scope.

The following code works perfectly:

```
dt <- data.table(mpg)
list <- levels(dt$manufacturer)

dt[manufacturer==list[3]]

```

If I then try to loop through the list with a for loop: [updated code - **solved**]

```
for (var in list) {
  subs <- data.table(melt(dt[manufacturer==var, list(model, hwy, cty)], id.vars='model'))

  png(paste(var, 'png', sep='.'))
  print(ggplot(subs, aes(model, value, col=variable)) + geom_point())
  dev.off()
}

```

I do not get anything.

For me the code parts look identical and should work equally. Does anyone have a suggestion what might cause that I do not get anything with the second filtering ? Any help is appreciated.