Reputation: 291
I have 2 lists of dataframes. I want to create a function that returns a vector of indices of DF2 in which an index is a duplicate dataframe. I need to compare every df in list 2 against every df in list 1. This code also needs to work on equal length lists where list 1 may be longer than list 2.
For example,
library(tidyverse)
x <- mtcars %>% split(.$cyl)
y <- x[1:2]
y[[3]] <- mtcars[1:5,1:6]
x[[4]] <- x[[1]]
x[[5]] <- x[[2]]
identical(x[[1]],y[[1]]) #returns true
identical(x[[3]], y[[1]]) #returns false
length(x) #returns 5
length(y) #returns 3
Pseudo code:
comparelists <- function(x,y) {some code}
q <- mapply(compareLists, x, y)
q #returns 1,2
I believe I'll need multiple identical statements, but am having diffculties coming up with efficient logic to check every dataframe in list 2 against every data frame in list 1 such that list2[[[1]] is compared against list1[[1:5]] and list2[[2]] is compared against list1[[1:5]] and so on...
Upvotes: 2
Views: 245
Reputation: 887183
If we need to do this on each pairwise, use
lapply(x, function(u) sapply(y, identical, u))
Or with outer
outer(x, y, FUN = Vectorize(identical))
Upvotes: 3