title: Subset Data.Frame With Multiple Conditions
body: <h2>End Goal:</h2>
  
  <p>Create a plot for each region of StressCumulative, BaseCumulative, StressQoQ, and BaseQoQ over the date range from rows 1:167. </p>
  
  <h2>Problem:</h2>
  
  <p>I'm having difficulty subsetting my data.frame. My issue is that the condition by which I'm subsetting is logical, and thus will only return the first element after the condition.  </p>
  
  <pre><code>subset_region_1 &lt;- subset.data.frame(HPF, HPF$region == 1, select = BaseCumulative, HPF$StressCumulative, StressQoQ, BaseQoQ)
  
  Warning messages:
  1: In if (drop) warningc("drop ignored") :
    the condition has length &gt; 1 and only the first element will be used
  2: drop ignored 
  </code></pre>
  
  <p>This returns only the first column, BaseCumulative. </p>
  
  <h2>Data:</h2>
  
  <p>Here you get a glimpse of what I'm working with. This is the table I am looking to subset from. My data.frame is in a tall format 
  <a href="https://i.stack.imgur.com/T2IHc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T2IHc.png" alt="Region 1"></a>
  <a href="https://i.stack.imgur.com/oskDa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oskDa.png" alt="Region 2"></a></p>
  
  <p>I would like to create a subset in order to graph BaseCumulative, StressCumulative, BaseQoQ, and StressQoQ variables over the range of dates from rows 1:167. The date column uses the same dates for all 100 regions. My issue is that when I go to plot in ggplot, I get an error that my aes mappings are not of the same size. The full table has date = 18370 rows long, but the values repeat every 167 rows (for each unique region). Further, the BaseCumulative variable is also 18370 rows long but is unique for all regions, i.e. every 167 rows. I want to know how I can subset by region while obtaining the correct row size for the variables I am interested in measuring. </p>
  
  <h2>Data Pts:</h2>
  
  <pre><code>#Rows 1-3 (Region 1 Sample): 
  dput(head(HPF[1:3, ]))
      structure(list(region = c(1, 1, 1), path = c(1, 1, 1), date = c(20140215, 
      20140515, 20140815), index_value = c(1, 1.033852765, 1.041697122
      ), index = 0:2, counter = 1:3, BaseQoQ = c(NA, 0.033852765, 0.00758749917354029
      ), BaseCumulative = c(100, 103.3852765, 104.1697122), StressCumulative = c(110, 
      113.3852765, 114.1697122), StressQoQ = c(NA, 0.0307752409090909, 
      0.00691832065162346)), .Names = c("region", "path", "date", "index_value", 
      "index", "counter", "BaseQoQ", "BaseCumulative", "StressCumulative", 
      "StressQoQ"), row.names = c(NA, -3L), class = c("tbl_df", "tbl", 
      "data.frame"))
  
  #Rows 168:200 (Region 2 Sample):
  dput(head(HPF[168:200, ]))
      structure(list(region = c(2, 2, 2, 2, 2, 2), path = c(1, 1, 1, 
      1, 1, 1), date = c(20140215, 20140515, 20140815, 20141115, 20150215, 
      20150515), index_value = c(1, 1.014162265, 1.01964828, 1.009372314, 
      1.007210703, 1.018695493), index = 0:5, counter = 1:6, BaseQoQ = c(NA, 
      0.014162265, 0.00540940556489744, -0.0100779515854232, -0.0021415398163972, 
      0.0114025694582001), BaseCumulative = c(100, 101.4162265, 101.964828, 
      100.9372314, 100.7210703, 101.8695493), StressCumulative = c(110, 
      111.4162265, 111.964828, 110.9372314, 110.7210703, 101.8695493
      ), StressQoQ = c(NA, 0.0128747863636363, 0.00492389230216839, 
      -0.00917785181610786, -0.00194849914020834, -0.0799443229370588
      )), .Names = c("region", "path", "date", "index_value", "index", 
      "counter", "BaseQoQ", "BaseCumulative", "StressCumulative", "StressQoQ"
      ), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
      ))
  </code></pre>
  
  <h2>Question:</h2>
  
  <p>How do I subset other columns in addition to specifying region == #? I have tried the following but then the issue is that values recycle for the dates and my charts are incorrect: </p>
  
  <pre><code>ggplot(HPF, aes(x = date, y= BaseCumulative, linetype = factor(region == 1))) + 
    geom_line() +
    theme_light()
  </code></pre>
  
  <p>Further, I am also unsuccessful if I try to subset within the ggplot such as: </p>
  
  <pre><code>ggplot(HPF[HPF$region == 1, ], aes(x = HPF$date[1:167, ], y= HPF$BaseCumulative[1:167, ], linetype = factor(region == 1))) + 
        geom_line() +
        theme_light()
  </code></pre>
  
  <p>Any help is appreciated. </p>