[R] Is there way to add a new row to a data frame in a specific location

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 24 20:07:08 CET 2011


On Thu, 24 Nov 2011, Jeff Newmiller wrote:

> AFAIK all solutions to the "grow object size" problem in R involve 
> creation of a new object to "change" an old one.  There is 
> considerable sophistication under the hood that allows a minimum of 
> intermediate objects to be created if you are careful, but actually 
> changing the size of an object in place is not supported.

Just for the record, it actually is (R vectors have a LENGTH and 
TRUELENGTH property at C level), but this is AFAIK only used 
internally.

The better question is why you care about the order of the rows of a 
data frame?  A good way to think of a d.f. is like a table in a DBMS: 
for efficiency the cases are unordered, but you can retrieve them in 
any order you want (or no order).  Indeed, for efficient operations on 
millions of rows an R-DBMS interface is highly recomemded -- see the 
R-data manual.

>
> That doesn't mean that you have to "copy... to a temporary and then copy back", since you can reference chunks of an existing object without actually moving them in memory by using indexing. But (AFAIK) you cannot escape creating at least one new copy of the data that "becomes" the object if you use rbind to grow your object.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> Sammy Zee <szee2007 at gmail.com> wrote:
>
>> Is there easy way (without copying the existing rows to a temporary
>> location and copying back) to add a new row to a specific index
>> location in
>> an existing data frame?
>>
>> Example
>>
>> df = data.frame( A= c('a','b','c'), B=c(1,2,3), C=(10,20,30))
>>
>> newrow = c('X', 100, 200)
>>
>> I want to add the newrow as the second row to the data frame df
>>
>> Please suggest a solution that is efficient for a data frame that 
>> can have millions of rows, and I want to add a new row in any given 
>> index location of the data frame.
>>
>> Thanks,
>> Sammy

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list