[R] Insert rows in between dataframes
John Kane
jrkrideau at yahoo.ca
Thu Aug 20 16:18:37 CEST 2009
There may be a much cleaner and better way but just split the data.frame into two parts, create the NA rows as another data.frame and use rbind to put them back together again.
Example
#=======================================================================
(mydata <- data.frame(aa=letters[1:5], bb=1:5))
topdata <- mydata[1:3,]
bottomdata <- mydata[4:5,]
nadata <- data.frame(aa=rep(NA,2),bb=rep(NA,2))
d1 <- rbind(topdata,nadata)
d2 <- rbind(d1, bottomdata)
#=======================================================================
--- On Thu, 8/20/09, Moumita Das <das.moumita.online at gmail.com> wrote:
> From: Moumita Das <das.moumita.online at gmail.com>
> Subject: [R] Insert rows in between dataframes
> To: r-help at r-project.org
> Received: Thursday, August 20, 2009, 7:18 AM
> Hi all,
>
> Can anyone suggest me how to insert rows in between data
> frames and also
> keep the ordering of row numbers correct?
>
>
>
> Estimate
> Std. Error
> t
> value Pr(>|t|)
>
> recmeanC2
> 9.275880e-17
> 6.322780e-17 1.467057e+00
> 0.14349903
>
> recmeanC3
>
> 1.283534e-17 2.080644e-17
> 6.168929e-01
> 0.53781390
>
> recmeanC4 -
> 3.079466e-17
> 2.565499e-17 -1.200338e+00
> 0.23103743
>
> i1
>
> 5.000000e-01
> 1.036197e-17 4.825338e+16
> 0.00000000
>
> i2
>
> -5.630739e-18
> 1.638267e-17 -3.437010e-01
> 0.73133282
>
> i3
>
> 4.291387e-18
> 1.207522e-17 3.553879e-01
> 0.72257050
>
> i4
>
> 1.472662e-17
> 1.423051e-17 1.034863e+00
> 0.30163897
>
> i5
>
> 5.000000e-01
> 1.003323e-17
> 4.983441e+16
> 0.00000000
>
> i6
>
> 5.147966e-18
> 1.569095e-17
> 3.280850e-01
> 0.74309614
>
> i7
>
>
> 1.096044e-17 1.555829e-17
> 7.044760e-01
> 0.48173041
>
> i8
>
> -1.166290e-18
> 1.287370e-17
> -9.059482e-02 0.92788026
>
> i9
>
>
> 1.627371e-17 1.540567e-17
> 1.056345e+00
> 0.29173427
>
> i10
>
>
> 4.001692e-18 1.365740e-17
> 2.930053e-01
> 0.76973827
>
> i12
>
>
> -1.052843e-17 1.324484e-17
> -7.949081e-01 0.42735000
>
> i13
>
>
> 2.571236e-17 1.357336e-17
> 1.894325e+00
> 0.05922715
>
> This is my coeffcients table.Here I want ti inser t
> rows with value NA in
> all columns in between and after the last row.After
> last row I can insert
> using rbind.But how will I enter row in between so that the
> order also
> doesn’t change.
>
> I need this to be generated :---
>
> Estimate
> Std. Error
> t value Pr(>|t|)
>
> recmeanC2
> 9.275880e-17
> 6.322780e-17 1.467057e+00
> 0.14349903
>
> recmeanC3
>
> 1.283534e-17 2.080644e-17
> 6.168929e-01
> 0.53781390
>
> recmeanC4 -
> 3.079466e-17
> 2.565499e-17 -1.200338e+00
> 0.23103743
>
> i1
>
> 5.000000e-01
> 1.036197e-17 4.825338e+16
> 0.00000000
>
> i2
>
> -5.630739e-18
> 1.638267e-17 -3.437010e-01
> 0.73133282
>
> i3
>
> 4.291387e-18
> 1.207522e-17 3.553879e-01
> 0.72257050
>
> i4
>
> 1.472662e-17
> 1.423051e-17 1.034863e+00
> 0.30163897
>
> i5
>
> 5.000000e-01
> 1.003323e-17
> 4.983441e+16
> 0.00000000
>
> i6
>
> 5.147966e-18
> 1.569095e-17
> 3.280850e-01
> 0.74309614
>
> i7
>
>
> 1.096044e-17 1.555829e-17
> 7.044760e-01
> 0.48173041
>
> i8
>
> -1.166290e-18
> 1.287370e-17
> -9.059482e-02 0.92788026
>
> i9
>
>
> 1.627371e-17 1.540567e-17
> 1.056345e+00
> 0.29173427
>
>
> i10
>
>
> 4.001692e-18 1.365740e-17
> 2.930053e-01
> 0.76973827
>
> i11
>
> NA
> NA
>
> NA
> NA
>
> i12
>
>
> -1.052843e-17 1.324484e-17
> -7.949081e-01 0.42735000
>
> i13
>
>
> 2.571236e-17 1.357336e-17
> 1.894325e+00
> 0.05922715
>
> i14
>
> NA
> NA
>
> NA
> NA
>
>
>
> i15
>
> NA
> NA
>
> NA
> NA
>
>
>
>
>
> rbind after row number i10 will change row number
> i12 to have NA,Na,NA.Na
> values.
>
>
> --
> Thanks in advance
> Moumita
>
> [[alternative HTML version deleted]]
>
>
> -----Inline Attachment Follows-----
>
> ______________________________________________
> 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.
>
__________________________________________________________________
Make your browsing faster, safer, and easier with the new Internet Explorer® 8. Optimize
explorer/
More information about the R-help
mailing list