[R] using the function unique(), but asking it to ignore a column of a data.frame

Andrew Yee yee at post.harvard.edu
Mon Jul 9 17:07:39 CEST 2007


Thanks, this works perfectly.

On 7/9/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Mon, 9 Jul 2007, Andrew Yee wrote:
>
> > Thanks.  But in this specific case, I would like the output to include
> > all three columns, including the "ignored" column (in this case, I'd
> > like it to ignore column a).
>
> sample.data.frame[!duplicated(sample.data.frame[-1]), ]
>
> (index to exclude columns as you wish).
>
> > Thanks,
> > Andrew
> >
> > On 7/9/07, hadley wickham <h.wickham at gmail.com> wrote:
> >> On 7/9/07, Andrew Yee <yee at post.harvard.edu> wrote:
> >>> Take for example the following data.frame:
> >>>
> >>> a<-c(1,1,5)
> >>> b<-c(3,2,3)
> >>> c<-c(5,1,5)
> >>> sample.data.frame<-data.frame(a=a,b=b,c=c)
> >>>
> >>> I'd like to be able to use unique(sample.data.frame), but have
> >>> unique() ignore column a when determining the unique elements.
> >>>
> >>> However, I figured that this would be setting for incomparables=, but
> >>> it appears that this funcationality hasn't been incorporated.  Is
> >>> there a work around for this, i.e. to be able to get unique to only
> >>> look at selected columns of a data frame?
> >>
> >> unique(df[,c("a","c")]) ?
> >>
> >> Hadley
> >>
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>



More information about the R-help mailing list