Use an 'apply' function to perform code with conditional statements in R
<p>I have been working on a project for which I need to find peaks and valleys in a dataset (not just the highest numbers per column, but all of the peaks and valleys). 
I did manage to get it to work on 1 column, but I use a for-loop for that and I need to do this for about 50 columns, so I think I should use an 'apply' function. I just don't know how to do so. Can I put 'if' statements and such in an 'apply' function?</p>

<p>Here is what I used for checking one column:
('First' is the name of the dataset and 'Seq1' is the first column)</p>

<pre><code>Lowest = 0
Highest = 0
Summits = vector('numeric')
Valleys = vector('numeric')

for (i in 1:length(First$Seq1))
{
    if (!is.na(First$Seq1[i+1]))
    {
        if (First$Seq1[i] &lt; Lowest) {Lowest = First$Seq1[i]}
        if (First$Seq1[i] &gt; Highest) {Highest = First$Seq1[i]}  

        if (First$Seq1[i] &gt; 0 &amp;&amp; First$Seq1[i+1] &lt; 0) 
        { Summits &lt;- append(Summits, Highest, after=length(Summits)) }
        if (First$Seq1[i] &lt; 0 &amp;&amp; First$Seq1[i+1] &gt; 0) 
        { Valleys &lt;- append(Valleys, Lowest, after=length(Summits)) }
    }
}
</code></pre>