[R] Insert row in specific location between data frames

David L Carlson dcarlson at tamu.edu
Fri Jun 29 21:39:32 CEST 2012


Here you want the name of the column, not the data frame:

nadata <- data.frame(predict_SO2_a=NA)

instead of 

nadata <- data.frame(dat1=NA)

Then it will work.


-------
David

> -----Original Message-----
> From: arun [mailto:smartpink111 at yahoo.com]
> Sent: Friday, June 29, 2012 2:01 PM
> To: dcarlson at tamu.edu
> Cc: R help
> Subject: Re: [R] Insert row in specific location between data frames
> 
> Hi David,
> 
> I am getting error messages with the code.
> dat1<-read.table(text="
> predict_SO2_a
> 1      39.793231
> 2      30.252578
> 3      32.467584
> 4      31.941509
> 5      27.908320
> 6      11.594137
> 7        9.368125
> 8      12.319093
> 9      11.558811
> 10      7.937192
> 11      11.211306
> 12      12.400342
> 13      12.393146
> 14      13.256160
> 15      10.709600
> 16      9.966334
> 17      28.850652
> 18      10.024405
> ",sep="",header=TRUE)
> 
> 
> topdata <- dat1[1:10, , drop=FALSE]
> bottomdata <- dat1[11:nrow(dat1), , drop=FALSE]
> nadata <- data.frame(dat1=NA)
> d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)
> 
> 
> Error in match.names(clabs, names(xi)) :
>   names do not match previous names
> 
> 
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: David L Carlson <dcarlson at tamu.edu>
> To: 'Peter Ehlers' <ehlers at ucalgary.ca>; 'pigpigmeow'
> <glorykwok at hotmail.com>
> Cc: r-help at r-project.org
> Sent: Friday, June 29, 2012 2:28 PM
> Subject: Re: [R] Insert row in specific location between data frames
> 
> You are having the problem because you have a one column data frame.
> When
> you extract that column, R converts it to a vector. You can insert the
> NA
> into the vector and then convert it to a data.frame or you can prevent
> R
> from converting the data.frame to a vector and insert the row using a
> slightly modified version of your code:
> 
> # Prevent conversion to a vector with drop=FALSE
> topdata <- predict_SO2_a[1:10, , drop=FALSE]
> bottomdata <- predict_SO2_a[11:nrow(predict_SO2_a), , drop=FALSE]
> 
> # Assign a column name in nadata so rbind works properly
> nadata <- data.frame(predict_SO2_a=NA)
> 
> # skip d1 step and merge everything at once. rbind() gives you a
> # data frame, but the row.names are messed up, this command fixes that
> d2 <- data.frame(rbind(topdata, nadata, bottomdata), row.names=NULL)
> 
> ----------------------------------------------
> David L Carlson
> Associate Professor of Anthropology
> Texas A&M University
> College Station, TX 77843-4352
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Peter Ehlers
> > Sent: Friday, June 29, 2012 1:04 PM
> > To: pigpigmeow
> > Cc: r-help at r-project.org
> > Subject: Re: [R] Insert row in specific location between data frames
> >
> >
> > On 2012-06-29 10:01, pigpigmeow wrote:
> > > Hi everyone! I have a simple question.
> > >
> > > my data is
> > >   predict_SO2_a
> > > 1       39.793231
> > > 2       30.252578
> > > 3       32.467584
> > > 4       31.941509
> > > 5       27.908320
> > > 6       11.594137
> > > 7        9.368125
> > > 8       12.319093
> > > 9       11.558811
> > > 10       7.937192
> > > 11      11.211306
> > > 12      12.400342
> > > 13      12.393146
> > > 14      13.256160
> > > 15      10.709600
> > > 16       9.966334
> > > 17      28.850652
> > > 18      10.024405
> > >
> > >
> > >
> > > I want to insert row which is "NA" in 10th row
> >
> > Try this:
> >
> >    d <- data.frame(x = 101:118, y = rnorm(18))
> >    d2 <- data.frame(
> >               rbind(head(d, 9), NA, tail(d, -9)),
> >               row.names = NULL)
> >
> > Peter Ehlers
> >
> > >
> > > that is ..
> > >   predict_SO2_a
> > > 1       39.793231
> > > 2       30.252578
> > > 3       32.467584
> > > 4       31.941509
> > > 5       27.908320
> > > 6       11.594137
> > > 7        9.368125
> > > 8       12.319093
> > > 9       11.558811
> > > 10     NA
> > > ....
> > > and it becomes 19 rows in this data.
> > >
> > >
> > > however, I can't do this. my scipt is following
> > > topdata<- predict_SO2_a[1:10,]
> > > bottomdata<- predict_SO2_a[11:nrow(predict_SO2_a),]
> > > nadata<- data.frame(NA)
> > > d1<- rbind(topdata,nadata)
> > > d2<- rbind(d1, bottomdata)
> > >
> > > what is my problem?!
> > >
> > > Thank in advance!
> > >
> > >
> > >
> > >
> > > --
> > > View this message in context: http://r.789695.n4.nabble.com/Insert-
> > row-in-specific-location-between-data-frames-tp4634905.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > ______________________________________________
> > > 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.
> >
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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