[R] extracting duplicated elements

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 23 18:06:11 CEST 2007


Try:

 lapply(as.data.frame(t(DF)), function(x) unique(x[duplicated(x) & x > 0]))


On 8/23/07, dxc13 <dxc13 at health.state.ny.us> wrote:
>
> Can anyone help me solve this problem...thanks!
>
> Consider a data frame, namely v, as such:
> > v
>   X1 X2 X3 X4 X5 X1 X2 X3 X4 X5
> x1  1  2 -1 -1 -1  1  2 -1 -1 -1
> y1  1  2 -1 -1 -1  1  2  3 -1 -1
>
> What I would like to do is to create an array or data frame with only the
> elements that appear in the data frame more than once and are >= 0.
>
> I try this...
> > v[v>=0]
> [1] 1 1 2 2 1 1 2 2 3
>
> which returns all >= 0 elements, but they are not in their respective rows
> from the original data frame.  I have tried using the duplicated()
> function and can't seem to get it to work correctly.
>
> Essentially, the outcome I am trying to get is a df or array looking like:
>
> step 1...achieve this out of original df
> [1] 1 2 1 2
> [2] 1 2 1 2 3
>
> (the blank element in row 1, position 5 can be just be NA)
>
> step 2...take the above and get this...only the duplicated elements
> [1] 1 2
> [2] 1 2
>
> --
> View this message in context: http://www.nabble.com/extracting-duplicated-elements-tf4318034.html#a12295213
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list