[R] Count Unique Rows/Values

Liaw, Andy andy_liaw at merck.com
Thu Apr 20 17:50:53 CEST 2006


From:Sachin J
> 
> Hi,
>    
>   This one is not working for me. It is listing all the rows 
> instead of unique ones. My dataset has 300000 odd rows and 
> following is the resulting o/p
>   
> [[308313]]
> [1] 126
>   [[308314]]
> [1] 126
>   [[308315]]
> [1] 126
>   [[308316]]
> [1] 126
>   [[308317]]
> [1] 126
>   [[308318]]
> [1] 126
>   [[308319]]
> [1] 126
>   [[308320]]
> [1] 126
>   [[308321]]
> [1] 126
>    
>   I used following set of commands.
>    
>   > (x.unique <- lapply(x$V1, unique))

You want "x" instead of "x$V1" as the first argument to lapply(), so that it
runs unique() on all columns of "x".

Andy


> > sapply(x.unique, length)
>    
>   x$V1 is numeric field.
>   where x is my data frame already read (therefore i ignored 
> your first step). Am I missing something. ?
>    
>   Thanks
>   Sachin
> 
> "Liaw, Andy" <andy_liaw at merck.com> wrote:
>   This might help:
> 
> > x <- read.table("clipboard", colClasses=c("numeric", "character")) 
> > (x.unique <- lapply(x, unique))
> $V1
> [1] 155 138 126 123 103 143 111 156
> 
> $V2
> [1] "A" "B" "C" "D"
> 
> > sapply(x.unique, length)
> V1 V2 
> 8 4 
> 
> Andy
> 
> From: Sachin J
> > 
> > Hi,
> > 
> > I have a dataset which has both numeric and character
> > values with dupllicates. For example:
> > 
> > 155 A
> > 138 A
> > 138 B
> > 126 C
> > 126 D
> > 123 A
> > 103 A
> > 103 B
> > 143 D
> > 111 C
> > 111 D
> > 156 C
> > 
> > How can I count the number of unqiue entries without
> > counting duplicate entries. Also can I extract the list in a 
> > object. What I mean is
> > Col1 unique count = 8 Unique Elements are : 
> > 103,111,123,126,138,143,155,156
> > Col2 unique count = 4 Unique Elements are : A,B,C,D.
> > 
> > Any pointers would be of great help.
> > 
> > TIA
> > Sachin
> > 
> > 
> > 
> > ---------------------------------
> > 
> > [[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > 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
> > 
> > 
> 
> 
> --------------------------------------------------------------
> ----------------
> 
> --------------------------------------------------------------
> ----------------
> 
> 
> 		
> ---------------------------------
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
> 
>




More information about the R-help mailing list