patrickroocks / listcompr

An R package for list comprehension

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

list vs. data.frame in gen.data.frame

ggrothendieck opened this issue · comments

(1) below works as expected but if we replace data.frame with list giving (2) it gives a different answer. Is this expected?

library(lubridate)
library(magrittr)
library(listcompr)

my_interval = interval(dmy("15/07/2019"), dmy("15/07/2020"))
my_intervals <- rep(my_interval, 3)

# 1
my_intervals %>% { 
    n <- length(.)
    gen.data.frame(data.frame(int1 = .[i], int2 = .[j], 
      overlaps = int_overlaps(.[1], .[j])), i < j, i = 1:n, j = 1:n)
  }

                        int1                           int2 overlaps
## 1 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE
## 2 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE
## 3 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE


# 2 - same except data.frame replaced with list on second last line
my_intervals %>% { 
    n <- length(.)
    gen.data.frame(list(int1 = .[i], int2 = .[j], 
      overlaps = int_overlaps(.[1], .[j])), i < j, i = 1:n, j = 1:n)
  }
##       int1     int2 overlaps
## 1 31622400 31622400     TRUE
## 2 31622400 31622400     TRUE
## 3 31622400 31622400     TRUE

No this is not intended, internally the as.data.frame call happens too late... I will fix that soon. Thanks for that finding!

Fixed with the last commit.

Now we have:

> library(lubridate)
> library(magrittr)
> 
> my_interval = interval(dmy("15/07/2019"), dmy("15/07/2020"))
> my_intervals <- rep(my_interval, 3)
> my_intervals %>% { 
+   n <- length(.)
+   gen.data.frame(list(int1 = .[i], int2 = .[j], 
+                       overlaps = int_overlaps(.[1], .[j])), i < j, i = 1:n, j = 1:n)
+ }
                            int1                           int2 overlaps
1 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE
2 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE
3 2019-07-15 UTC--2020-07-15 UTC 2019-07-15 UTC--2020-07-15 UTC     TRUE