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.
On 10/09/2015 9:11 AM, 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.Do you want 1,2,1 to be the same as 1,1,2, or different? What about 1,2,2? For sets, those are all the same, but for most purposes, they aren't. If you really want to keep the ordering, then table() does the counting you want, it just returns it in an ugly format. Duncan Murdoch> > 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. >
Dear Thomas, How about this?> table(apply(Data, 1, paste, collapse=","))1,1,1 1,2,1 1,3,2 1 2 1 I hope this helps, John> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Thomas > Chesney > Sent: September 10, 2015 9:11 AM > To: r-help at r-project.org > Subject: [R] Counting occurrences of a set of values > > 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.
Have a look at the dplyr package library(dplyr) n <- 1000 data_frame( V1 = sample(0:1, n, replace = TRUE), V2 = sample(0:1, n, replace = TRUE), V3 = sample(0:1, n, replace = TRUE) ) %>% group_by(V1, V2, V3) %>% mutate( Freq = n() ) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey 2015-09-10 15:11 GMT+02:00 Thomas Chesney <Thomas.Chesney at nottingham.ac.uk>:> 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. >[[alternative HTML version deleted]]
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. >