Create a new dataframe column with the first element of elements in another column in R : <p>I want to create a new column in my dataframe such that it is formed by the first element (or 2 elements) of elements in another column in the same dataframe. E.g.</p>

<pre><code>   columnA
1   "1234"
2   "9876"
3   "4567"
</code></pre>

<p>Turns into:</p>

<pre><code>   columnA    columnB
1   "1234"       "12"
2   "9876"       "98"
3   "4567"       "45"
</code></pre>

<p>I tried with the dplyr library like this:</p>

<pre><code>dataframe %&gt;%
    mutate( columnB = columnA[1:2] )
</code></pre>

<p>But this is trying to retrieve the first two rows.</p>

<p>If anyone knows anyway to do this fast (preferably with dplyr library), I would appreciate it a lot. Thanks in advance.</p>
