[R] rbind: inconsistent behaviour with empty data frames?
David L Carlson
dcarlson at tamu.edu
Wed Jan 2 22:25:11 CET 2013
What version of R are you using?
I cannot replicate your results with R 2.15.2:
> df1 <- data.frame()
> df2 <- data.frame(foo=c(1, 2), bar=c("a", "b"))
> rbind(df1, df2)
foo bar
1 1 a
2 2 b
> df1 <- data.frame(matrix(0, 0, 2))
> names(df1) <- names(df2)
> rbind(df1, df2)
foo bar
1 1 a
2 2 b
----------------------------------------------
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 Dmitrii I.
> Sent: Wednesday, January 02, 2013 2:09 PM
> To: r-help at r-project.org
> Subject: [R] rbind: inconsistent behaviour with empty data frames?
>
> The rbind on empty and nonempty data frames behaves inconsistently. I
> am
> not sure if by design.
>
> In the first example, first row is deleted, which may or may not be on
> purpose:
> df1 <- data.frame()
> df2 <- data.frame(foo=c(1, 2), bar=c("a", "b"))
> rbind(df1, df2)
> foo bar
> 2 2 b
>
> Now if we continue:
> df1 <- data.frame(matrix(0, 0, 2))
> names(df1) <- names(df2)
> foo bar
> 2 1 a
> 3 2 b
> So now data frames combined successfully, but the row names have been
> increased by one.
>
> Finally, if row names are strings, it works okay:
> row.names(df2) <- c("row1", "row2")
> rbind(df1, df2)
> foo bar
> row1 1 a
> row2 2 b
>
> Is this behaviour by design?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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