[R] Add blank rows to a dataframe
Duncan Murdoch
murdoch.duncan at gmail.com
Sat Jun 8 13:29:03 CEST 2013
On 13-06-08 7:09 AM, Bert Jacobs wrote:
> Hi,
>
> I have a vector that looks like this:
> RowSel <-c(0,1,0,1,2,3,0,5,5)
>
> Now I want to select rows from a specific dataframe DF based on that vector
> like this:
> SubDF <- DF[RowSel,]
>
> So this works fine, but I was wondering how I could add blank rows add the
> locations in the vector where there is a zero:
> So the final dataframe should look like this:
>
> SubDF
> [1] blank row
> [2] row 1
> [3] blank row
> [4] row 1
> [5] row 2
> [6] row 3
> [7] blank row
> [8] row 5
> [9] row 5
>
> Do I have to use a loop for this or does there exist a straight forward
> function option.
You don't need a loop. Do it in two steps: add the blank row to the
end of the original dataframe (or a copy of it), then replace your 0
index with the row number of that row.
Duncan Murdoch
More information about the R-help
mailing list