'How to use string value as a column selector in a for loop [duplicate]

I have a for loop that looks like this

for (j in c("Month_1", "Month_2", "Month_3", "Month_4")) {
  r <- ggplot(df_tableAcc[df_tableAcc$j > 100,]) + 
    aes_string(x = "ShipSog") + 
    geom_bar()
  
  print(r)
}

Where the column names in df_tableAcc is "Month_1", "Month_2", "Month_3", "Month_4".

How can I use the "[df_tableAcc$j > 100," ?

I keep getting this error:

Warning: Unknown or uninitialised column: j. Error: Must subset rows with a valid subscript vector. i Logical subscripts must match the size of the indexed input. x Input has size 1852 but subscript df_tableAcc$j > 100 has size 0.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source