Page 1 of 1

Does data frame in R allow duplicate rows after merge or rbind?

Posted: October 28th, 2010, 12:40 pm
by HorseRider
I constructed a data frame as the following:n = c(2, 3, 5) s = c("aa", "bb", "cc") b = c(TRUE, FALSE, TRUE) df = data.frame(n, s, b)dfI want to check the row with s == "aa"df[df$s=="aa",]which gives n s b1 2 aa TRUENow I tried to add another row with same data.aRow = c(2, "aa", TRUE)rbind(df, aRow)and then rundf[df$s=="aa",]which only gives one row.nrow(df) gives 3 which is the same as before the row was added.But if you constructed with duplicates rows, it seems OK.nn = c(2, 3, 2, 5) ss = c("aa", "bb", "aa", "cc") bb = c(TRUE, FALSE, TRUE, TRUE) df2 = data.frame(nn, ss, bb)df2nrow(df2) # gives 4df2[df2$ss=="aa",] # gives 2 rowsThis is something caused subtle bug I found when I debug other's R code. She uses "merge" instead of "rbind" though.I am new to R. Any comment will be appreciated.

Does data frame in R allow duplicate rows after merge or rbind?

Posted: October 28th, 2010, 12:57 pm
by CurtHagenlocher
QuoteOriginally posted by: HorseRiderNow I tried to add another row with same data.aRow = c(2, "aa", TRUE)rbind(df, aRow)rbind doesn't update its argument; it returns a new data frame. You need to reassign the result back to df:df = rbind(df, aRow)

Does data frame in R allow duplicate rows after merge or rbind?

Posted: October 28th, 2010, 1:10 pm
by HorseRider
I see. thank you.