Sammy Zee
2011-Nov-24 17:05 UTC
[R] Is there way to add a new row to a data frame in a specific location
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]]
andrija djurovic
2011-Nov-24 17:19 UTC
[R] Is there way to add a new row to a data frame in a specific location
Hi. May be this: df = data.frame( A=c('a','b','c'), B=c(1,2,3), C=c(10,20,30), stringsAsFactors=FALSE) newrow = c('X', 100, 200) rbind(df,newrow)[c(1,4,2,3),] Andrija On Thu, Nov 24, 2011 at. 6:05 PM, Sammy Zee <szee2007@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@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. >[[alternative HTML version deleted]]
Jeff Newmiller
2011-Nov-24 18:23 UTC
[R] Is there way to add a new row to a data frame in a specific location
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.
Ian Strang
2011-Nov-25 19:10 UTC
[R] Is there way to add a new row to a data frame in a specific location
This look really interesting but I don't understand what is happening. Please can someone explain the last line and what the bit in [] is doing. Ian df = data.frame( A=c('a','b','c'), B=c(1,2,3), C=c(10,20,30), stringsAsFactors=FALSE) newrow = c('X', 100, 200) rbind(df,newrow)[c(1,4,2,3),]
Possibly Parallel Threads
- With an example - Re: rbind.data.frame drops attributes for factor variables
- rbind.data.frame drops attributes for factor variables
- R: "in-place" appending to a matrix.
- Losing custom attributes
- splitting a dataframe in R based on multiple gene names in a specific column