Interval sets algebra in R (union, intersection, differences, inclusion, …)

前端 未结 1 1682
别跟我提以往
别跟我提以往 2021-02-06 12:07

I am wondering whether a proper framework for interval manipulation and comparison does exist in R.

After some search, I was only able to find the following: - function

相关标签:
1条回答
  • 2021-02-06 12:44

    I think you might be able to make good use of the many interval-related functions in the sets package.

    Here's a small example illustrating the package's support for interval construction, intersection, set difference, union, and complementation, as well as its test for inclusion in an interval. These and many other related functions are documented on the help page for ?interval.

    library(sets)
    i1 <- interval(1,6)
    i2 <- interval(5,10)
    i3 <- interval(200,400)
    i4 <- interval(202,402)
    i5 <- interval_union(interval_intersection(i1,i2), 
                         interval_symdiff(i3,i4))
    
    i5
    # [5, 6] U [200, 202) U (400, 402]
    interval_complement(i5)
    # [-Inf, 5) U (6, 200) U [202, 400] U (402, Inf]
    
    interval_contains_element(i5, 5.5)
    # [1] TRUE
    interval_contains_element(i5, 201)
    # [1] TRUE
    

    If your intervals are currently encoded in a two-column data.frame, you could use something like mapply() to convert them to intervals of the type used by the sets package:

    df   <- data.frame(lBound = c(1,5,100), uBound = c(10, 6, 200))
    Ints <- with(df, mapply("interval", l=lBound, r=uBound, SIMPLIFY=FALSE))
    Ints
    # [[1]]
    # [1, 10]
    
    # [[2]]
    # [5, 6]
    
    # [[3]]
    # [100, 200]
    
    0 讨论(0)
提交回复
热议问题