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
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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With