Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a row with values belongs to a data frame in R [duplicate]

Tags:

r

Possible Duplicate:
Existing function for seeing if a row exists in a data frame?

Suppose I have the following data frame in R.

df = data.frame('a'=c(1:3), 'b'=c(4:6))

This data frame contains three rows: (1,4), (2,5) and (3,6). Suppose I did not know which rows df contains and wanted to check whether a row (1,4) belongs to it, how can I check that?

My actual case involves comparison of 27 parameter values. Is there a solution in which I can do this without typing each and every parameter name? Thanks!

The reason I want to do this is that I have an R dataset called masterdata which contains simulation data. I want to update this data set with new data that is obtained as I make additional simulation runs with different parameter combinations. It is possible, however, that I may forget that I have run the simulation for a certain parameter combination and may run it again, in which case, the masterdata will be expanded with duplicate values. I can later go and remove these duplicate values, but I would not want the whole process of updating the data set to go through if the values are duplicate. For this I need to check if the data from a simulation run is already present in the masterdata. I can do this if I know how to check whether a given row belongs to the masterdata.

Thanks.

like image 526
Curious2learn Avatar asked Jan 20 '23 12:01

Curious2learn


1 Answers

There may be more efficient ways, but I think

tail(duplicated(rbind(masterdata,newvals)),1)

will do it: in other words, attach the new row to the end of the data frame and see whether it is duplicated or not.

like image 151
Ben Bolker Avatar answered Jan 30 '23 18:01

Ben Bolker