Hi I have the following symmetric correlation matrix:

df <- data.frame(
A = c(1,0,2,3,0),
B = c(0,1,0,0,0),
C = c(2,0,1,4,0),
D = c(3,0,4,1,0)
E = c(0,0,0,0,1)
)
row.names(df) <- c('A','B','C', 'D', 'E')
df
A B C D E
A 1 0 2 3 0
B 0 1 0 0 0
C 2 0 1 4 0
D 3 0 4 1 0
E 0 0 0 0 1

You can see that rows/columns B and E have all zeros except for the identity; I would like to remove these rows/columns so the following matrix is left:

A C D
A 1 2 3
C 2 1 4
D 3 4 1

Thanks for any help you can provide. I have tried for 2 hours now to solve this and I can not. I Tried telling it to delete rowsums == 1, then colsums ==0 but this didn't work either.

df <- data.frame(
A = c(1,0,2,3,0),
B = c(0,1,0,0,0),
C = c(2,0,1,4,0),
D = c(3,0,4,1,0),
E = c(0,0,0,0,1)
)
row.names(df) <- c('A','B','C', 'D', 'E')
i_keep <- c()
for (col in names(df)){
if (sum(df[col]) !=1 ){
i_keep <- c(i_keep, col)
}
}
df[i_keep,i_keep]
#> A C D
#> A 1 2 3
#> C 2 1 4
#> D 3 4 1

^{Created on 2020-04-11 by the reprex package (v0.3.0)}

I'd caution,

This might not work on all matrices if values can be >1,