An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071221/195e4a12/attachment.pl
number of count of each unique row
4 messages · Louis Martin, G. Jay Kerns, Jim Lemon +1 more
1 day later
Hi Louis, If I am understanding your question correctly, here is one way: suppose your matrix is M of dimension n x k. D <- data.frame(M) # convert to data frame ones <- rep(1, n) # a column of 1s Now you can count the number of repeats of each unique row. aggregate( ones, by = as.list(D), FUN = sum) The output will be a data frame with the unique rows, and a column at the end labeled "x" with the frequency of each unique row. Once you get this you can convert to a list, manipulate, etc. I am sure that there exist faster/better methods. Best, Jay
On Dec 21, 2007 5:03 PM, Louis Martin <louismartinbis at yahoo.fr> wrote:
Hi,
I have a matrix of duplicate rows. How to output a list the unique rows with their count? I have used "unique" to have the unique rows, but can't produce the occurences of each unique row.
Thanks
Louis
---------------------------------
[[alternative HTML version deleted]]
______________________________________________ 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.
*************************************************** G. Jay Kerns, Ph.D. Assistant Professor / Statistics Coordinator Department of Mathematics & Statistics Youngstown State University Youngstown, OH 44555-0002 USA Office: 1035 Cushwa Hall Phone: (330) 941-3310 Office (voice mail) -3302 Department -3170 FAX E-mail: gkerns at ysu.edu http://www.cc.ysu.edu/~gjkerns/
Louis Martin wrote:
Hi, I have a matrix of duplicate rows. How to output a list the unique rows with their count? I have used "unique" to have the unique rows, but can't produce the occurences of each unique row.
Hi Louis,
If you want the unique rows returned, this might do the job.
unique.rows<-function(x) {
nrows<-dim(x)[1]
urows<-1:nrows
for(i in 1:(nrows-1)) {
for(j in (i+1):nrows) {
if(!is.na(urows[j])) if(all(x[i,]==x[j,])) urows[j]<-NA
}
}
return(x[urows[!is.na(urows)],])
}
Jim
i didn't test, but i think you want something like: table(apply(x, 1, paste, collapse=",")) where "x" is your matrix... b
On Dec 23, 2007, at 5:01 AM, Jim Lemon wrote:
Louis Martin wrote:
Hi, I have a matrix of duplicate rows. How to output a list the unique rows with their count? I have used "unique" to have the unique rows, but can't produce the occurences of each unique row.
Hi Louis,
If you want the unique rows returned, this might do the job.
unique.rows<-function(x) {
nrows<-dim(x)[1]
urows<-1:nrows
for(i in 1:(nrows-1)) {
for(j in (i+1):nrows) {
if(!is.na(urows[j])) if(all(x[i,]==x[j,])) urows[j]<-NA
}
}
return(x[urows[!is.na(urows)],])
}
Jim
______________________________________________ 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.