Onless Onless - 2 months ago 7
R Question

Getting columns with equivalent values in rows

I need to get from a number of rows where some columns are equivalent and extract exactly those columns.

I have the following dataframe:

a <- c(1,2,3)
b <- c(1,2,3)
c <- c(4,5,6)
A <- data.frame(a,b,c)

> A
a b c d
1 1 2 4 1
2 2 2 5 2
3 3 3 6 3


I would like the following result:

> columnInnerJoin(A)
a d
1 1 1
2 2 2
3 3 3


Or, more specifically:

> columnInnerJoinGiveColumns(A)
a d

Answer

We can try with duplicated

res <- A[duplicated(as.list(A))|duplicated(as.list(A), fromLast=TRUE)]
names(res)
#[1] "a" "d"