[R] detect repeated number in a vector

2008-10-08 Thread liujb

Dear R users,

I have this vector that consists numeric numbers. Is there a command that
detects the repeated numbers in a vector and returns the index of the
repeated numbers (or the actual numbers)? For example, v - c(3,4,5,7,4).
The command would return me index 2 and 5 (or the repeated number, 4).

Thank you very much,
Julia
-- 
View this message in context: 
http://www.nabble.com/detect-repeated-number-in-a-vector-tp19884768p19884768.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] detect repeated number in a vector

2008-10-08 Thread Duncan Murdoch

On 08/10/2008 2:36 PM, liujb wrote:

Dear R users,

I have this vector that consists numeric numbers. Is there a command that
detects the repeated numbers in a vector and returns the index of the
repeated numbers (or the actual numbers)? For example, v - c(3,4,5,7,4).
The command would return me index 2 and 5 (or the repeated number, 4).


duplicated() comes close, but the first occurence doesn't count as a 
duplication:


 duplicated(v)
[1] FALSE FALSE FALSE FALSE  TRUE

To convert into values, you can index v by it:

 v[duplicated(v)]
[1] 4

and to find which indices are duplicated,

 indices - seq_along(v)
 indices[duplicated(v)]
[1] 5

If you really want to include the the first one, you can do something like:

 indices[ v %in% v[duplicated(v)] ]
[1] 2 5


Duncan Murdoch

__
R-help@r-project.org 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.


Re: [R] detect repeated number in a vector

2008-10-08 Thread N. Lapidus
Can this be an answer ?

which(v %in% names(table(v)[table(v)1]))
[1] 2 5

Nael

On Wed, Oct 8, 2008 at 8:36 PM, liujb [EMAIL PROTECTED] wrote:


 Dear R users,

 I have this vector that consists numeric numbers. Is there a command that
 detects the repeated numbers in a vector and returns the index of the
 repeated numbers (or the actual numbers)? For example, v - c(3,4,5,7,4).
 The command would return me index 2 and 5 (or the repeated number, 4).

 Thank you very much,
 Julia
 --
 View this message in context:
 http://www.nabble.com/detect-repeated-number-in-a-vector-tp19884768p19884768.html
 Sent from the R help mailing list archive at Nabble.com.

 __
 R-help@r-project.org 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] detect repeated number in a vector

2008-10-08 Thread liujb

Duncan,

Thank you so much. It is exactly what I was looking for. However, for some
reason, indices() does not work (Error: could not find function indices).
I used which(), it worked. 

Thanks again for the help,
Julia



Duncan Murdoch-2 wrote:
 
 On 08/10/2008 2:36 PM, liujb wrote:
 Dear R users,
 
 I have this vector that consists numeric numbers. Is there a command that
 detects the repeated numbers in a vector and returns the index of the
 repeated numbers (or the actual numbers)? For example, v - c(3,4,5,7,4).
 The command would return me index 2 and 5 (or the repeated number, 4).
 
 duplicated() comes close, but the first occurence doesn't count as a 
 duplication:
 
   duplicated(v)
 [1] FALSE FALSE FALSE FALSE  TRUE
 
 To convert into values, you can index v by it:
 
   v[duplicated(v)]
 [1] 4
 
 and to find which indices are duplicated,
 
   indices - seq_along(v)
   indices[duplicated(v)]
 [1] 5
 
 If you really want to include the the first one, you can do something
 like:
 
   indices[ v %in% v[duplicated(v)] ]
 [1] 2 5
 
 
 Duncan Murdoch
 
 __
 R-help@r-project.org 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.
 
 

-- 
View this message in context: 
http://www.nabble.com/detect-repeated-number-in-a-vector-tp19884768p19887359.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.