# Re: [R] unique columns in a matrix and number of each

From: Sunayan Acharya <sunayan_at_gmail.com>
Date: Sun, 23 Nov 2008 04:57:18 -0500

maybe there's a much simpler way to do this but here's what I got:

> z

[,1] [,2] [,3] [,4] [,5] [,6]

```[1,]    1    1    4    4    4    4
[2,]    2    2    5    5    5    5
[3,]    3    3    6    6    6    6
```

> z.unique<-unique(z,MARGIN=2)
> z.unique

[,1] [,2]

```[1,]    1    4
[2,]    2    5
[3,]    3    6
```

> z.unique.dup<-array(apply(z.unique,2,function(x)
rep(x,ncol(z))),dim=c(nrow(z),ncol(z),ncol(z.unique)))
> z.unique.dup

, , 1

[,1] [,2] [,3] [,4] [,5] [,6]

```[1,]    1    1    1    1    1    1
[2,]    2    2    2    2    2    2
[3,]    3    3    3    3    3    3

```

, , 2

[,1] [,2] [,3] [,4] [,5] [,6]

```[1,]    4    4    4    4    4    4
[2,]    5    5    5    5    5    5
[3,]    6    6    6    6    6    6

```

> apply(z.unique.dup,3,function(x) length(which(colSums(x==z)==nrow(z))))
[1] 2 4

This gives the number of times each column in z.unique occurs in z in the order of the columns of z.unique.

On Sun, Nov 23, 2008 at 12:19 AM, jim holtman <jholtman_at_gmail.com> wrote:

> 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
> 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?
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[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. Received on Sun 23 Nov 2008 - 09:59:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 23 Nov 2008 - 10:30:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.