Home > Software engineering >  Comparing unique values across multiple data frames in R
Comparing unique values across multiple data frames in R

Time:03-24

I have 3 data frames (df1,df2,df3), each with 1274 rows and 2192 columns. I want to count the number of occurrences when the value of a cell matches 0.968 in df1, 0.972 in df2 and 0.909 in df3. Note that the cells have to be in the exact same location (same row and column number).

Example,

df1

| 0.968 | 0.526 |
| 0.938 | 0.632 |
| 0.873 | 0.968 |

df2

| 0.342 | 0.972 |
| 0.545 | 0.231 |
| 0.434 | 0.972 |

df3

| 0.673 | 0.812 |
| 0.128 | 0.764 |
| 0.909 | 0.909 |

The answer should return: 1

Is using a loop the best option to solve this?

CodePudding user response:

You can try the code below

sum(df1==0.968 & df2 == 0.972 & df3 = 0.909)

If you would like to index the TRUE values, you can use which

which(df1==0.968 & df2 == 0.972 & df3 = 0.909, arr.ind = TRUE)

CodePudding user response:

Another possible solution:

sum((df1 == 0.968) *  (df2 == 0.972) * (df3 == 0.909))

#> [1] 1
  • Related