Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

dplyr - Counting the common indices shared between columns of a dataframe containing only binary values in R

Suppose I have a dataframe containing binary values as:

     A    B    C    D
a    1    0    0    0
b    0    1    1    0
c    1    1    0    1
d    0    0    1    1
e    1    1    1    1
f    1    0    0    1 

I'd like to count the number of common indices shared between the pair of columns in the dataframe as shown below. What is the most efficient way to do so in R ?

     A    B    C    D
A    -    2    1    3
B    2    -    2    2
C    1    2    -    2
D    3    2    2    -

Any help is greatly appreciated. Thanks!

question from:https://stackoverflow.com/questions/66048949/counting-the-common-indices-shared-between-columns-of-a-dataframe-containing-onl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe this is what you are after

> `diag<-`(crossprod(as.matrix(df)),NA)
   A  B  C  D
A NA  2  1  3
B  2 NA  2  2
C  1  2 NA  2
D  3  2  2 NA

Data

> dput(df)
structure(list(A = c(1L, 0L, 1L, 0L, 1L, 1L), B = c(0L, 1L, 1L, 
0L, 1L, 0L), C = c(0L, 1L, 0L, 1L, 1L, 0L), D = c(0L, 0L, 1L,
1L, 1L, 1L)), class = "data.frame", row.names = c("a", "b", "c",
"d", "e", "f"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...