[R] rbind()

Yevgeniy Kirpichevsky kirpich at fas.harvard.edu
Sat Jul 24 06:58:56 CEST 2004


hi.  I'm merging two datasets.
one of them is 51 rows, and a typical row looks like this:

midwar[midwar$dispnum==89,]
    dispnum synch sanum saname sbnum sbname year1 yearn ainit binit fatala
158      89     0   220    FRN   230    SPN  1822  1823     1     1      4
     fatalb       key1       keyn warnum year1.war yearn.war awon
158      5 2202301822 2202301823      1      1823      1823    2

The other is 3 rows, and it looks like this:
   dispnum synch sanum saname sbnum sbname year1 yearn ainit binit fatala 
fatalb
7      NA    NA    70    MEX   220    FRN    NA    NA    NA    NA     NA     NA
   key1 keyn warnum year1.war yearn.war awon
7   NA   NA     40      1862      1867    2

So the column names are identical, and rbind() shouldn't create 
problems.  Here's what happens now when I call this:
midwar[midwar$dispnum==89,]

      dispnum synch sanum saname sbnum sbname year1 yearn ainit binit fatala
158       89     0   220    FRN   230    SPN  1822  1823     1     1      4
NA        NA    NA    NA   <NA>    NA   <NA>    NA    NA    NA    NA     NA
NA.1      NA    NA    NA   <NA>    NA   <NA>    NA    NA    NA    NA     NA
NA.2      NA    NA    NA   <NA>    NA   <NA>    NA    NA    NA    NA     NA
      fatalb       key1       keyn warnum year1.war yearn.war awon
158       5 2202301822 2202301823      1      1823      1823    2
NA       NA         NA         NA     NA        NA        NA   NA
NA.1     NA         NA         NA     NA        NA        NA   NA
NA.2     NA         NA         NA     NA        NA        NA   NA

It gives me extra 3 rows of NAs for each row of the original data set.  The 
only thing that I can think of that may be going on is that in the first 
data set the names of counties are vectors of factors, and in the second 
data set they are characters.  Could that be causing a problem?  I can't 
check, because I can't make factor vectors into character vectors 
(as.character() didn't work).

Sorry for the rambling question.  Any insights are appreciated,

-yev




More information about the R-help mailing list