Search code examples
rlistcomparison

Comparisons of more than two lists in R


For my problem, I have 4 lists, each containing 10,000 elements. Let the lists be a, b, c, d. I can calculate Probablity(a<b) by just performing mean operation mean(a<b). If I understand correctly, it compares each of the 10,000 elements in a and b in order and tells me for how many elements a<b holds (with the number being divided by total elements).

Now, I want to compute Probablity(a<b<c<d). I want r to compare 10,000 elements in order and tell me for how many elements does (a<b<c<d) hold. However, I'm unable to do it using the mean function since it doesn't accept more than one < sign. How can I use the mean function here? I'm an absolute beginner in r, but logically, I feel this should be straightforward rather than looping over everything and having a count variable.


Solution

  • How about this:

    a <- runif(10000)
    b <- runif(10000)
    c <- runif(10000)
    d <- runif(10000)
    mean(a<b & b<c & c<d)
    #> [1] 0.0425
    

    Created on 2022-12-06 by the reprex package (v2.0.1)