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

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Thu Nov 24 19:23:57 CET 2011


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.

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
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list