I have a square matrix of dimension N. I want to define a vector of size N that has as first component: all row indices of the rows of the matrix which are the same as the first row. and as second component: all row indices of the rows of the matrix which are the same as the second row.
and so on.
I am working on R and I have been trying to do that for some time now. Any idea on how to proceed would me much appreciated.
myMatrix <- matrix(rep(1:4, 4), ncol = 2, byrow = FALSE)
[,1] [,2]
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 1 1
[6,] 2 2
[7,] 3 3
[8,] 4 4
What I have tried :
res <- list(NA)
for (i in 1:nrow(myMatrix)) {
row_selected <- myMatrix[i,]
res[[i]] <- which(myMatrix[i,]==row_selected)
}
res
The dplyr
version:
# turn the matrix into a dataframe
myDf <- myMatrix %>% as.data.frame()
myDf %>% # and now get a left join of ...
left_join(
myDf %>% # ...the same dataframe with the index you were looking for
distinct_all() %>%
mutate(index = 1:nrow(.)))