Check if the corresponding elements in two different list of dataframes are equal

Check if the corresponding elements in two different list of dataframes are equal

Problem Description:

I have a two separate list of dataframes.
The first list of dataframe is lst1

$`1`
 [1] 8.523769e-05 6.615035e-05 1.177740e-04 1.173578e-04 8.798214e-05 6.328218e-05 4.768889e-05 3.658710e-05
 [9] 1.294623e-05 1.179838e-06 1.010590e-06 5.204994e-07 2.128048e-06 6.876634e-06 6.011750e-06 5.160903e-05
[17] 5.318722e-05 1.029438e-04 1.173397e-04

$`2`
 [1] 9.206525e-05 1.472897e-04 2.650569e-04 2.313355e-04 1.984725e-04 1.645034e-04 1.456210e-04 6.768821e-05
 [9] 4.187954e-05 4.823388e-05 4.279551e-05 4.191838e-05 5.642628e-05 8.094616e-05 1.013678e-04 2.232152e-04
[17] 2.255819e-04 2.358519e-04 2.645930e-04

$`3`
 [1] 0.0003263243 0.0009461198 0.0011599270 0.0009269332 0.0007441947 0.0021864953 0.0023836129 0.0019477809
 [9] 0.0006745046 0.0004784856 0.0005603616 0.0005089821 0.0005140971 0.0001900949 0.0002591991 0.0004950753
[17] 0.0005729661 0.0006404173 0.0007336286

The second list of dataframe is lst2

$`1`
 [1] 8.523769e-05 6.595870e-04 1.434451e-03 1.267164e-03 1.140118e-03 1.037180e-03 1.007861e-03 7.581415e-04
 [9] 1.294623e-05 1.179838e-06 1.010590e-06 5.204994e-07 2.128048e-06 5.297516e-04 5.515672e-04 5.510878e-04
[17] 5.635382e-04 6.966825e-04 7.091103e-04

$`2`
 [1] 1.335764e-03 1.534220e-03 7.072944e-04 2.313355e-04 1.984725e-04 1.645034e-04 1.456210e-04 6.768821e-05
 [9] 4.187954e-05 4.823388e-05 4.279551e-05 4.191838e-05 5.642628e-05 8.094616e-05 1.013678e-04 2.232152e-04
[17] 2.255819e-04 8.953077e-04 9.560716e-04

$`3`
 [1] 0.0003263243 0.0009461198 0.0011599270 0.0023806365 0.0030363610 0.0033131562 0.0027954296 0.0016694135
 [9] 0.0002395362 0.0002608362 0.0005603616 0.0005089821 0.0005140971 0.0001900949 0.0002591991 0.0004950753
[17] 0.0007601880 0.0009152222 0.0010781228

I am trying to check if the corresponding elements in lst1 is equal to lst2.
If the elements are equal in the list it should return x, if not it should return o.

The solution for such example can be

$`1`
[1] 0 x x x x .....and so on
...

I tried writing a function to do that.

checkreferencevalue<- function (ref,attk){ifelse(ref == attk, 'x','o')}

But I cannot seem to make it work.

Solution – 1

You can use this:

c("o", "x")[mapply(identical, lst1, lst2) + 1]

Reproducible demo:

lst1 = list(1:3, 4:6, 7:9)
lst2 = list(1:3, c(5, 5, 5), 7:9)
c("o", "x")[mapply(identical, lst1, lst2) + 1]
# [1] "x" "o" "x"

For element-wise comparison within the lists:

mapply((x, y) c("o", "x")[(x == y) + 1], lst1, lst2, SIMPLIFY = FALSE)
# [[1]]
# [1] "x" "x" "x"
# 
# [[2]]
# [1] "o" "x" "o"
# 
# [[3]]
# [1] "x" "x" "x"
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject