Hi, The unique function is easy to understand and use. Beyond that, I want to get also the frequency of repetition of each individual row in a data frame Let me explain with an example : x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) xu<-unique(x) We have,> x? a b? c 1 1 2 10 2 2 3 20 3 3 4 30 4 1 2 10 5 2 3 20> xu? a b? c 1 1 2 10 2 2 3 20 3 3 4 30 I want to get the following data frame : ? a b? c??? Freq 1 1 2 10??? 2 2 2 3 20??? 2 3 3 4 30??? 1 That is, in addition to the unique rows, I want to get the frequency of repetion of each individual row. I will appreciate all the help that I can get. Thank You, Ravi
Hi Ravi, Try this: x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) cbind(unique(x),lapply(x,table)$c)[,-4] a b c Freq 1 1 2 10 2 2 2 3 20 2 3 3 4 30 1 HTH, Jorge On Wed, May 7, 2008 at 12:11 PM, ravi <rv15i@yahoo.se> wrote:> Hi, > The unique function is easy to understand and use. Beyond that, I want to > get also the frequency of repetition of each individual row in a data frame > Let me explain with an example : > x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) > xu<-unique(x) > We have, > > x > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > 4 1 2 10 > 5 2 3 20 > > xu > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > > I want to get the following data frame : > a b c Freq > 1 1 2 10 2 > 2 2 3 20 2 > 3 3 4 30 1 > That is, in addition to the unique rows, I want to get the frequency of > repetion of each individual row. > I will appreciate all the help that I can get. > Thank You, > Ravi > > ______________________________________________ > 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]]
ravi - This may get you started count.reps <- function(df) { hash <- do.call("paste", c(df, sep = "\r")) cbind(unique(df), Freq = unclass(table(hash))) } test <- data.frame(a = rep(1:10, 2), b = rep(1:10, 2)) count.reps(test) Best, Erik Iverson ravi wrote:> Hi, > The unique function is easy to understand and use. Beyond that, I want to get also the frequency of repetition of each individual row in a data frame > Let me explain with an example : > x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) > xu<-unique(x) > We have, >> x > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > 4 1 2 10 > 5 2 3 20 >> xu > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > > I want to get the following data frame : > a b c Freq > 1 1 2 10 2 > 2 2 3 20 2 > 3 3 4 30 1 > That is, in addition to the unique rows, I want to get the frequency of repetion of each individual row. > I will appreciate all the help that I can get. > Thank You, > Ravi > > ______________________________________________ > R-help at 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. >
ravi wrote:> Hi, > The unique function is easy to understand and use. Beyond that, I want to get also the frequency of repetition of each individual row in a data frame > Let me explain with an example : > x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) > xu<-unique(x) > We have, >> x > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > 4 1 2 10 > 5 2 3 20 >> xu > a b c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > > I want to get the following data frame : > a b c Freq > 1 1 2 10 2 > 2 2 3 20 2 > 3 3 4 30 1 > That is, in addition to the unique rows, I want to get the frequency of repetion of each individual row. > I will appreciate all the help that I can get. > Thank You, > RaviAs usual in R, there are several options: > table(apply(x, 1, paste, collapse = ",")) 1,2,10 2,3,20 3,4,30 2 2 1 or: > table(interaction(x, sep = ",", drop = TRUE)) 1,2,10 2,3,20 3,4,30 2 2 1 So: > cbind(unique(x), Freq = as.vector(table(apply(x, 1, paste, collapse = ",")))) a b c Freq 1 1 2 10 2 2 2 3 20 2 3 3 4 30 1 or: > cbind(unique(x), Freq = as.vector(table(interaction(x, sep = ",", drop = TRUE)))) a b c Freq 1 1 2 10 2 2 2 3 20 2 3 3 4 30 1 HTH, Marc Schwartz
Another way to produce the data frame is subset(as.data.frame(table(x)),Freq>0) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 7 May 2008, ravi wrote:> Hi, > The unique function is easy to understand and use. Beyond that, I want to get also the frequency of repetition of each individual row in a data frame > Let me explain with an example : > x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) > xu<-unique(x) > We have, >> x > ? a b? c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > 4 1 2 10 > 5 2 3 20 >> xu > ? a b? c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > > I want to get the following data frame : > ? a b? c??? Freq > 1 1 2 10??? 2 > 2 2 3 20??? 2 > 3 3 4 30??? 1 > That is, in addition to the unique rows, I want to get the frequency of repetion of each individual row. > I will appreciate all the help that I can get. > Thank You, > Ravi > > ______________________________________________ > R-help at 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. >
Hi, Thanks for the different suggestions - Jorge, Erik, Marc, Ted and Phil. Thanks to you, I have learnt quite a few, for me new, tricks and methods. Thanks, Ravi ----- Original Message ---- From: Phil Spector <spector at stat.Berkeley.EDU> To: ravi <rv15i at yahoo.se> Cc: r-help at r-project.org Sent: Wednesday, 7 May, 2008 7:14:28 PM Subject: Re: [R] help with the unique function Another way to produce the data frame is subset(as.data.frame(table(x)),Freq>0) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? - Phil Spector ??? ??? ??? ??? ??? Statistical Computing Facility ??? ??? ??? ??? ??? Department of Statistics ??? ??? ??? ??? ??? UC Berkeley ??? ??? ??? ??? ??? spector at stat.berkeley.edu On Wed, 7 May 2008, ravi wrote:> Hi, > The unique function is easy to understand and use. Beyond that, I want to get also the frequency of repetition of each individual row in a data frame > Let me explain with an example : > x<-data.frame(a=c(1,2,3,1,2),b=c(2,3,4,2,3),c=c(10,20,30,10,20)) > xu<-unique(x) > We have, >> x > ? a b? c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > 4 1 2 10 > 5 2 3 20 >> xu > ? a b? c > 1 1 2 10 > 2 2 3 20 > 3 3 4 30 > > I want to get the following data frame : > ? a b? c??? Freq > 1 1 2 10??? 2 > 2 2 3 20??? 2 > 3 3 4 30??? 1 > That is, in addition to the unique rows, I want to get the frequency of repetion of each individual row. > I will appreciate all the help that I can get. > Thank You, > Ravi > > ______________________________________________ > R-help at 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. >
Ravi, if you have a large data.frame you might want to have a look at the count.rows function I collected from older threads and put into the wiki (http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:count_and_extract_unique_rows) WIth table I run into memory trouble - just as with aggregate. Claudia -- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 2 I-34127 Trieste phone: +39 (0 40) 5 88-34 47 email: cbeleites at units.it