Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select unique columns in an R matrix

Tags:

r

matrix

I want to select the unique columns in the matrix six on a simulation as follows:

> set.seed(3)
> sam = replicate(100, sample(1:3, 4, rep = T))
> (six = sam[,colSums(sam)==6])
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    2    1    1    1    1    1    1    1    2     1
[2,]    2    2    3    1    1    2    2    1    2     2
[3,]    1    1    1    1    2    1    1    3    1     1
[4,]    1    2    1    3    2    2    2    1    1     2

I would like to end up with a matrix as:

     [,1] [,2] [,3] [,4] [,5]  [,6] 
[1,]    2    1    1    1    1    1         
[2,]    2    2    3    1    1    1       
[3,]    1    1    1    1    2    3       
[4,]    1    2    1    3    2    1        
like image 240
Antoni Parellada Avatar asked Jan 09 '17 03:01

Antoni Parellada


People also ask

How do I find unique columns in R?

To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.

How do I select multiple columns in a matrix in R?

R – Get Multiple Columns of Matrix To get multiple columns of matrix, specify the column numbers as a vector preceded by a comma, in square brackets, after the matrix variable name. This expression returns the required columns as a matrix.

How do I select a specific column in a matrix in R?

You should therefore use a comma to separate the rows you want to select from the columns. For example: my_matrix[1,2] selects the element at the first row and second column. my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.

How do I pull unique rows in R?

To extract the unique rows of a data frame in R, use the unique() function and pass the data frame as an argument and the method returns unique rows.


2 Answers

Use unique function with MARGIN=2 and it will return a matrix with duplicated columns removed, by default, unique removes duplicated rows:

unique(six, MARGIN = 2)

#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    2    1    1    1    1    1
#[2,]    2    2    3    1    1    1
#[3,]    1    1    1    1    2    3
#[4,]    1    2    1    3    2    1
like image 122
Psidom Avatar answered Oct 12 '22 13:10

Psidom


We can use duplicated

six[,!duplicated(t(six))]
#    [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    2    1    1    1    1    1
#[2,]    2    2    3    1    1    1
#[3,]    1    1    1    1    2    3
#[4,]    1    2    1    3    2    1
like image 41
akrun Avatar answered Oct 12 '22 14:10

akrun