[R] Counting occurrences of a set of values

Frank Schwidom schwidom at gmx.net
Thu Sep 10 19:35:34 CEST 2015



df <- data.frame( V1= 1, V2= c( 2, 3, 2, 1), V3= c( 1, 2, 1, 1))
dfO <- df[ do.call( order, df), ]
dfOD <- duplicated( dfO)
dfODTrigger <- ! c( dfOD[-1], FALSE)
dfOCounts <- diff( c( 0, which( dfODTrigger)))
cbind( dfO[ dfODTrigger, ], dfOCounts)

  V1 V2 V3 dfOCounts
4  1  1  1         1
3  1  2  1         2
2  1  3  2         1

Regards


On Thu, Sep 10, 2015 at 01:11:24PM +0000, Thomas Chesney wrote:
> Can anyone suggest a way of counting how frequently sets of values occurs in a data frame? Like table() only with sets.
> 
> So for a dataset:
> 
> V1, V2, V3
> 1, 2, 1
> 1, 3, 2
> 1, 2, 1
> 1, 1, 1
> 
> The output would be something like:
> 
> 1,2,1: 2
> 1,3,2: 1
> 1,1,1: 1
> 
> Thank you,
> 
> Thomas Chesney
> 
> 
> 
> This message and any attachment are intended solely for the addressee
> and may contain confidential information. If you have received this
> message in error, please send it back to me, and immediately delete it. 
> 
> Please do not use, copy or disclose the information contained in this
> message or in any attachment.  Any views or opinions expressed by the
> author of this email do not necessarily reflect the views of the
> University of Nottingham.
> 
> This message has been checked for viruses but the contents of an
> attachment may still contain software viruses which could damage your
> computer system, you are advised to perform your own checks. Email
> communications with the University of Nottingham may be monitored as
> permitted by UK legislation.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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