How to remove rows in a dataframe considering there are duplicates in one column of dataframe

前端 未结 2 1478
庸人自扰
庸人自扰 2021-01-16 08:50

Hi dear I have a little problem with a dataframe that has duplicates in a column. I would like to remove the rows where a column presents duplicates. For example my datafram

2条回答
  •  一个人的身影
    2021-01-16 09:19

    The use of which should only be done with its "positive" version. The danger in using the construction -which() is that when none of the rows or items match the test, the result of the which() is numeric(0) and -numeric(0) will return 'nothing', when the correct result is 'everything'. Use use:

     dat[!duplicated(dat), ]  
    

    In this case there were no duplicated rows, but the OP thought that some should be removed so obviously it was only two or three columns were under consideration. This is easy to accommodate. Just do the duplication test on 2 or three columns:

     dat[ !duplicated(dat[ , 2:3] ) , ]
    

提交回复
热议问题