[R] index after rbind

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Apr 21 19:54:05 CEST 2003


Spencer Graves <spencer.graves at pdf.com> writes:

>  > df1 <- data.frame(a=1:2)
>  > df2 <- rbind(df1, df1)
>  > df2
>     a
> 1  1
> 2  2
> 11 1
> 22 2
>  > rownames(df2) <- 1:4
> 
> hope this helps. spencer graves

...and this is not a bug. Data frames should have unique rownames, so
rbind.data.frame does this internally:

    while (any(xj <- duplicated(rlabs))) rlabs[xj] <- paste(rlabs[xj],
        1:sum(xj), sep = "")

[Just for fun, have a look at 
  df1 <- data.frame(a=1:2)
  df2 <- rbind(df1, df1, df1)
  rbind(df2,df2)
]

However, it might be considered slightly unfortunate that there is no
way to turn this off, even when the next thing you're going to do is to
set new rownames.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907



More information about the R-help mailing list