Dmitrii I.
2013-Jan-02 20:08 UTC
[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]]
David L Carlson
2013-Jan-02 21:25 UTC
[R] rbind: inconsistent behaviour with empty data frames?
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.
Dmitrii Izgurskii
2013-Jan-03 19:43 UTC
[R] rbind: inconsistent behaviour with empty data frames?
> What version of R are you using?I am using version 2.15.1. Suddenly everything works correctly too. I guess a restarting R did the trick. On 01/02/2013 10:25 PM, David L Carlson wrote:> 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.
Apparently Analagous Threads
- merging or joining 2 dataframes: merge, rbind.fill, etc.?
- Windows 2000 crash while using rbind (PR#8225)
- Subassignments involving NAs in data frames
- rbind with partially overlapping column names
- Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?