I need help determining the unique columns of a matrix and the numbers of each unique column. For example, let's say I have a matrix with 6 columns, 2 of these are filled with the value of 1 and the other 4 are filled with the value of 0. I would then like to have a command that tells me what the unique columns are (so columns with 1s and columns with 0s) and the number of each type of column (so 2 columns of 1s and 4 columns of 0s) that occur in the matrix. Thank you for any help anyone can provide!! I have been very impressed with the help that I have received so far!! Thank you!
One way is to 'paste' together the values in a column and then use 'table' to count them. 'duplicated' can probably do the same thing with the MARGIN option to find the duplicated one. You still them have to find the original ones. On Sat, Nov 22, 2008 at 3:42 PM, Salas, Andria Kay <aks2515 at uncw.edu> wrote:> I need help determining the unique columns of a matrix and the numbers of each unique column. For example, let's say I have a matrix with 6 columns, 2 of these are filled with the value of 1 and the other 4 are filled with the value of 0. I would then like to have a command that tells me what the unique columns are (so columns with 1s and columns with 0s) and the number of each type of column (so 2 columns of 1s and 4 columns of 0s) that occur in the matrix. > > Thank you for any help anyone can provide!! I have been very impressed with the help that I have received so far!! Thank you! > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Gabor Grothendieck
2008-Nov-23 11:26 UTC
[R] unique columns in a matrix and number of each
Define inner as in: http://tolstoy.newcastle.edu.au/R/e4/help/08/08/19562.html and use that with identical to define an inner product: m <- matrix(c(1, 2, 1, 2, 3, 2), 2) # test matrix m colSums(inner(m, u <- unique(m,,2), identical)) u Please provide actual code to create test examples and expected results when you post. Read the last line to every message in r-help and the posting guide. On Sat, Nov 22, 2008 at 3:42 PM, Salas, Andria Kay <aks2515 at uncw.edu> wrote:> I need help determining the unique columns of a matrix and the numbers of each unique column. For example, let's say I have a matrix with 6 columns, 2 of these are filled with the value of 1 and the other 4 are filled with the value of 0. I would then like to have a command that tells me what the unique columns are (so columns with 1s and columns with 0s) and the number of each type of column (so 2 columns of 1s and 4 columns of 0s) that occur in the matrix. > > Thank you for any help anyone can provide!! I have been very impressed with the help that I have received so far!! Thank you! > ______________________________________________ > 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. >