Serving the Quantitative Finance Community

 
User avatar
HorseRider
Topic Author
Posts: 0
Joined: December 8th, 2004, 4:12 am

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

October 28th, 2010, 12:40 pm

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.
 
User avatar
CurtHagenlocher
Posts: 0
Joined: May 26th, 2010, 12:42 pm

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

October 28th, 2010, 12:57 pm

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)
 
User avatar
HorseRider
Topic Author
Posts: 0
Joined: December 8th, 2004, 4:12 am

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

October 28th, 2010, 1:10 pm

I see. thank you.