2021年4月1日星期四

How to compare every row of dataframe to dataframe in R?

I want to get the number of values that are equal to every other row in dataframe:

library(tidyverse)    df <- tibble(    a = c(1, 1, 5, 1),    b = c(2, 3, 2, 8),    c = c(2, 6, 2, 2)  )  

desired output:

# A tibble: 4 x 4        a     b     c desired_column    <dbl> <dbl> <dbl> <list>          1     1     2     2 <dbl [4]>       2     1     3     6 <dbl [4]>       3     5     2     2 <dbl [4]>       4     1     8     2 <dbl [4]>     

enter image description here

In the column "desired_column": firt row: 3, 1, 2, 2:

3: is because the first row has the same three values compared to itself

1: is because there is one value with the same value in both rows and same column (first and second):

enter image description here

2: There are two values that are equal in first and third row and same column :

enter image description here

2: There are two values that are equal in first and fourth row and same column :

enter image description here

The second, third and fourth row of "desired_column" are results of the same process: The ith number in the result is the number of values in common between the current row and the ith row

https://stackoverflow.com/questions/66913605/how-to-compare-every-row-of-dataframe-to-dataframe-in-r April 02, 2021 at 09:59AM

没有评论:

发表评论