Filippo Gambarota
Filippo Gambarota

Reputation: 301

How to compare rows with each other in a dataframe in R

I have a dataframe final_matrix generated from all permutation without replacement of 8.

require(tidyverse)
require(gtools)

set.seed(222)

x <- 0:7

totale <- permutations(8,8,0:7, repeats.allowed = F)

final_matrix <- as.data.frame(sample_n(as_tibble(totale), 120, replace = F))

I would be sure that each row is different so I'm trying to write a loop that check each row with other so 1 vs 2, 1 vs 3... and then 2 vs 3 ... The comparing function is:

isTRUE(all.equal(prova%>% slice(#rownumber), prova %>% slice(#rownumber)))

All nested loops that I've tried haven't worked, how can I do?

Upvotes: 1

Views: 48

Answers (1)

ClancyStats
ClancyStats

Reputation: 1231

There are two quick ways to check this type of condition for a matrix or dataframe. The unique and duplicated functions work for these structures on a row-by-row basis. Thus, you can check this condition with

nrow(final_matrix) == nrow(unique(final_matrix))

or even more simply,

sum(duplicated(final_matrix)) == 0

Upvotes: 2

Related Questions