I guess rbind takes attributes from the first dataframe. I tried to rbind with mydata[NULL,,drop=FALSE] but rbind help states that empy data frames are dropped from rbind. So that as a workaround, I have:
mydata*<-* rbind(mydata[1,,drop=FALSE],newrow, mydata)[-1,] Eric On 23 November 2011 13:20, Smart Guy <smartgu...@gmail.com> wrote: > Hi All, > I was adding a new row of data to my data frame using rbind(). I > was surprised to see that after adding new row, I lost my data frame level > attibute as well as col level attribute. Please help me to insert a new > row at frist or middle position so that my custom attribute is not lost. > > Here is what I did. > > age<-c(15,20,18) > weight<-c(40,42,30) > > ### creating my data frame #### > mydata <- data.frame(age,weight) > > ### creating data frame level attribute #### > attr(mydata,"myattr")<-c("myinfo") > > ### creating col level attribute for 'age' column ### > attr(mydata$age,"mycolattr")<-c("mycolinfo") > > #### Checking attributes ### > attributes(mydata) > attributes(mydata$age) > > ### creating new row ##### > newrow <- data.frame(age=16, weight= 42) > > #### Inserting newrow as first row to my data frame #### > mydata<- rbind(newrow, mydata) > > #### Checking attributes again ### I lost my custom attributes > attributes(mydata$age) > attributes(mydata) > > > Thanks in advance, > > > -- > SG > > [[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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > -- Eric Lecoutre Consultant - Business & Decision Business Intelligence & Customer Intelligence [[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.