1

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.

Gornil
  • 27
  • 4

1 Answers1

1

Change df_tableAcc$j to df_tableAcc[[j]]:

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)
}
Aron Strandberg
  • 3,040
  • 9
  • 15