Peter Dalgaard
2004-Dec-23 11:01 UTC
[R] subsetting a data.frame to the 'unique' of a column
Rudi Alberts <r.alberts at rug.nl> writes:> something like this, however, this gives me the complete df. > > df[df$colname %in% unique(df$colname),] > > or this, which doesnt work > > df[df$colname == unique(df$colname),]df[!duplicated(df$colname),] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Göran Broström
2004-Dec-23 11:14 UTC
[R] subsetting a data.frame to the 'unique' of a column
On Thu, Dec 23, 2004 at 11:28:31AM -0800, Rudi Alberts wrote:> Hi, > > I often run into this problem: > I have a data.frame with one column containing entries that are not > unique. What I then want is a subset of the data.frame in which > the entries in that column have become the 'unique' of the original > column. > Normally I program around it by taking the unique of the column and > making a new data.frame with it and filling the rest of the data. > > (By the way, when moving to the smaller data.frame for example 5 rows > with the same value in that column will be replaced by one row for that > value. I don't mind which of the rows now..) > > > something like this, however, this gives me the complete df. > > df[df$colname %in% unique(df$colname),] > > or this, which doesnt work > > df[df$colname == unique(df$colname),] >Use 'duplicated':> df[!duplicated(df$colname), ]-- G??ran Brostr??m tel: +46 90 786 5223 Department of Statistics fax: +46 90 786 6614 Ume?? University http://www.stat.umu.se/egna/gb/ SE-90187 Ume??, Sweden e-mail: gb at stat.umu.se
Rudi Alberts
2004-Dec-23 19:28 UTC
[R] subsetting a data.frame to the 'unique' of a column
Hi, I often run into this problem: I have a data.frame with one column containing entries that are not unique. What I then want is a subset of the data.frame in which the entries in that column have become the 'unique' of the original column. Normally I program around it by taking the unique of the column and making a new data.frame with it and filling the rest of the data. (By the way, when moving to the smaller data.frame for example 5 rows with the same value in that column will be replaced by one row for that value. I don't mind which of the rows now..) something like this, however, this gives me the complete df. df[df$colname %in% unique(df$colname),] or this, which doesnt work df[df$colname == unique(df$colname),] regards, R. Alberts