Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

dataframe - Compare group of two columns and return index matches R

Many thanks for reading. Apologies for what I'm sure is a simple task.

I have a dataframe: (Edited: Added extra column not to be included in comparison)

b = c(5, 6, 7, 8, 10, 11) 
c = c('david','alan','pete', 'ben', 'richard', 'edd') 
d = c('alex','edd','ben','pete','raymond', 'alan')
df = data.frame(b, c, d) 
df
   b       c       d
1  5   david    alex
2  6    alan     edd
3  7    pete     ben
4  8     ben    pete
5 10 richard raymond
6 11     edd    alan

I want to compare the group of columns c and d with the group of columns d and c. That is, for one row, I want to compare the combined values in c and d with the combined values in d and c for all other rows.

(Note the values could either be characters or integers)

Where these match I want to return the index of those rows which match, preferably as a list of lists. I need to be able to access the indexes without referring to the values in column c or d.

I.e. for the above dataframe, my expected output would be:

c(c(2, 6), c(3, 4))
((2,6), (3,4))

As:

Row 2: (c + d == alan + edd) = row 6: (d + c == edd + alan)
Row 3: (c + d == pete + ben) = row 4: (d + c == ben + pete)

I understand how to determine the match case for two separate columns using match melt, but not if they are joined together and iterating over all possible row combinations.

I envision something like:

lapply(1:6, function(x), ifelse((df$a & df$b) == (df$b & df$a), index(x), 0))

But obviously that is incorrect and won't work.

I consulted the following questions but have been unable to formulate an answer. I have no idea where to begin.

Matching multiple columns on different data frames and getting other column as result

match two columns with two other columns

Comparing two columns in a data frame across many rows

R Comparing each value of all pairs of columns

How can I achieve the above?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could do something like this. It splits the row indices 1:nrow(df) according to unique sorted strings formed from the columns of df. The sorting ensures that A,B and B,A are treated identically.

duplist <- split(1:nrow(df),apply(df,1,function(r) paste(sort(r),collapse=" ")))

duplist
$`alan edd`
[1] 2 6

$`alex david`
[1] 1

$`ben pete`
[1] 3 4

$`raymond richard`
[1] 5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...