Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

r - Deleting rows that are duplicated in one column based on the conditions of another column

Here is an example of my data set;

Date      Time(GMT)Depth Temp  Salinity Density Phosphate
24/06/2002  1000    1           33.855          0.01
24/06/2002  1000    45          33.827          0.01
01/07/2002  1000    10  13.26   33.104  24.873  0.06
01/07/2002  1000    30  12.01   33.787  25.646  0.13
08/07/2002  1000    5   13.34   33.609  25.248  0.01
08/07/2002  1000    40  12.01   34.258  26.011  1.33
15/07/2002  1000    30  12.04   34.507  26.199  0.01
22/07/2002  1000    5   13.93   33.792  25.269  0.01
22/07/2002  1000    30  11.9    34.438  26.172  0.08
29/07/2002  1000    5   13.23   34.09   25.642  0.01

I want to delete duplicate rows so that I only have one row per date, I want to do this based on the Depth, I would like to keep the row with the greatest (deepest) depth. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Lets say you have data in df

df = df[order(df[,'Date'],-df[,'Depth']),]
df = df[!duplicated(df$Date),]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...