Skip to content

unique columns of a matrix

2 messages · Roman Akhter Ahmed, John Fox

#
Dear all,

I have a matrix of repeating columns in R, for example a matrix X is

          [,1]   [,2]   [,3]   [,4]
[1,]       1      1      1      1
[2,]       1      1      2      2

I want to store unique columns of the matrix X in a new matrix Y. 
Therefore, Y will be

          [,1]   [,2]  
[1,]       1      1    
[2,]       1      2

It will be really appreciated if you can provide me some function for 
this job.
Thanks for your time and effort in advance,
Roman
#
Dear Roman,

You can use unique(X, MARGIN=2). See ?unique for details.

I hope this helps,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------