I have two lists of data frame, with binary values in them.
As an example : The first list is lst1
and it contains the following values
$`4`
[1] "N" "N" "N" "A" "A" "A" "A" "N"
$`7`
[1] "N" "N" "N" "N" "N" "N" "N" "N"
The second list is lst2
and it contains the following values
$`4`
[1] "N" "N" "A" "A" "A" "A" "A" "N" "N" "N"
$`7`
[1] "N" "N" "N" "N" "N" "N" "N" "N" "N" "N"
I want to subtract the index of lst2
from lst1
at the first instance of "A"
.
So in lst1
, at dataframe $`4`
, the index value of "A"
is 4. similarly in lst2
at $`4`
, the index value of "A"
is 3 . The difference between them would be 4-3 = 1. And so on for other data frames in the list.
I have tried looking for a couple of solutions, but I am at my wit's end. Thank you!
CodePudding user response:
I wrote a function to calculate the expected difference. It returns a list with the same names as the argument lists, with the calculated difference.
Keep in mind two important things:
- The difference could not be calculated if "A" doesn't appear in any of the vectors (that's the case with vector
7
). - If names do not match, the calculation doesn't have any sense, that's why I throw an error (
stop
)
This is the function code:
calcDif = function(lst1, lst2){
if (!identical(names(lst1), names(lst2))){
stop("Names do not match!")
}
difs = list()
for (i in names(lst1)){
idx1 = min(which(lst1[[i]] == "A"))
idx2 = min(which(lst2[[i]] == "A"))
difs[[i]] = idx1 - idx2
}
return(difs)
}
If I run it with your data, I get:
calcDif(lst1, lst2)
$`4`
[1] 1
$`7`
[1] NaN
Warning messages:
1: In min(which(lst1[[i]] == "A")) :
no non-missing arguments to min; returning Inf
2: In min(which(lst2[[i]] == "A")) :
no non-missing arguments to min; returning Inf
If I attempt to run it with lists of unmatching names, then:
calcDif(list(x = 1:2), lst2)
Error in calcDif(list(x = 1:2), lst2) : Names do not match!