Subsetting by row value in R
<p>Firstly apologies if this has been answered else where I have searched all over for this but cannot find the answer. My problem may be due to the way I am searching for info and so I decided to use StackOverflow so I can present my problem with examples.</p>

<p>I have five minute OHLC data DIA_5.csv which I have then added a DayOfYear column to using Lubridate;</p>

<p><code>library(lubridate)</code>
<code>DIA_5[,6]&lt;- yday(DIA_5[,1])</code></p>

<p>Which looks like this; </p>

<pre><code>    Date                Open        High        Low         Close       DOY 
1   2015-09-21 09:30:00 164.6700    164.7100    164.3700    164.5300    264
2   2015-09-21 09:35:00 164.5300    164.9000    164.5300    164.6400    264
3   2015-09-21 09:40:00 164.6600    164.8900    164.6000    164.8900    264
4   2015-09-21 09:45:00 164.9100    165.0900    164.9100    164.9736    264
5   2015-09-21 09:50:00 164.9399    165.0980    164.8200    164.8200    264
</code></pre>

<p>What I wanted to do was create a new d$f with the first column starting with the individual day of year numbers, then I would fill this new d$f by subsetting data from the original OHLC d$f using the day of year number. The aim of this is so that in the new d$f I can extract the MAX value from all the highs on day x and bring it to column on the new d$f and so on with the other variables. The closest I could get to this goal was by using the following code however, this returns me all the values from the OHLC and I cannot find away to change this so that only the day of year number is brought across to the new d$f.</p>

<p><code>DF&lt;-DIA_5[match(unique(DIA_5[,6]), DIA_5[,6]),]</code></p>

<pre><code>  row.names DATE    OPEN    HIGH    LOW CLOSE   DOY
1   1   2015-09-21 09:30:00 164.67  164.7100    164.370 164.5300    264
2   79  2015-09-22 09:30:00 162.62  162.9600    162.620 162.7544    265
3   157 2015-09-23 09:30:00 163.26  163.3800    162.980 163.1400    266
4   235 2015-09-24 09:30:00 161.12  161.3700    161.060 161.2300    267
5   313 2015-09-25 09:30:00 163.81  163.9100    163.570 163.5800    268
</code></pre>

<p>Despite having more data than needed using the above code I decided to try subsetting the data. So from the above I wanted in the row next to 264 to use this value as a filter on the main OHLC d$f then extract the highest value in the column of highs. Using </p>

<pre><code>DF[,6] &lt;- max(subset(DIA_5[,3], yday(DIA_5[,1]) == DF[,6] ))
</code></pre>

<p>gave me</p>

<pre><code>Warning message:
In yday(DIA_5[, 1]) == DF[, 6] :
longer object length is not a multiple of shorter object length
</code></pre>

<p>It did give a new column  on d$f but this had the same value repeated.</p>

<pre><code>row.names   DATE    OPEN    HIGH    LOW CLOSE   DOY
1   1   2015-09-21 09:30:00 164.67  164.7100    164.370 164.5300    179.02
2   79  2015-09-22 09:30:00 162.62  162.9600    162.620 162.7544    179.02
3   157 2015-09-23 09:30:00 163.26  163.3800    162.980 163.1400    179.02
4   235 2015-09-24 09:30:00 161.12  161.3700    161.060 161.2300    179.02
5   313 2015-09-25 09:30:00 163.81  163.9100    163.570 163.5800    179.02
6   391 2015-09-28 09:30:00 162.04  162.0600    161.660 161.7100    179.02
</code></pre>

<p>I tried using my subset syntax to pull the max high value from a random DOY number and it seems to work fine;</p>

<pre><code>h &lt;- max(subset(DIA_5[,3], yday(DIA_5[,1]) == DF[1,6] ))
</code></pre>

<p>But I just cannot find out how to do this so that it creates a new column of the <code>MAX</code> value in the high column on <code>x</code> day of year.</p>

<p>Any help with this would be very much appreciated.</p>