Code Monkey home page Code Monkey logo

listcompr's People

Contributors

patrickroocks avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

statunizaga

listcompr's Issues

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

(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

combining columns

The following code works and gives the desired result.

nms <- names(BOD)
as.data.frame(gen.named.list("{nms[i]}.{nms[j]}", as.list(BOD)[[i]] + as.list(BOD)[[j]], i = 1:2, j = 1:2))
##   Time.Time demand.Time Time.demand demand.demand
## 1         2         9.3         9.3          16.6
## 2         4        12.3        12.3          20.6
## 3         6        22.0        22.0          38.0
## 4         8        20.0        20.0          32.0
## 5        10        20.6        20.6          31.2
## 6        14        26.8        26.8          39.6

however, it was necessary as far as I understand to convert the data frame to list and back if one wants to operate on columns rather than rows. I initially tried this but then realized it does not work as I had intended. Maybe there could be some facility to allow one to easily operate on columns and get columns?

gen.named.data.frame("{nms[i]}.{nms[j]}", BOD[[i]] + BOD[[j]], i = 1:2, j = 1:2)) # nms defined above

##                 V1   V2 V3 V4   V5   V6
## Time.Time      2.0  4.0  6  8 10.0 14.0
## demand.Time    9.3 12.3 22 20 20.6 26.8
## Time.demand    9.3 12.3 22 20 20.6 26.8
## demand.demand 16.6 20.6 38 32 31.2 39.6

gen.data.frame sometimes does not generate a data.frame

In the following example we used gen.data.frame but it produced a matrix.

a <- anscombe
gen.data.frame(data.frame(empty = NA, a[i], a[i+1]), i = seq(1, ncol(a), 2), bycol = TRUE)

giving this **matrix**

      empty x1 x2 empty x3 x4 empty    y1   y2 empty    y3    y4
 [1,]    NA 10 10    NA 10  8    NA  8.04 9.14    NA  7.46  6.58
 [2,]    NA  8  8    NA  8  8    NA  6.95 8.14    NA  6.77  5.76
 [3,]    NA 13 13    NA 13  8    NA  7.58 8.74    NA 12.74  7.71
 [4,]    NA  9  9    NA  9  8    NA  8.81 8.77    NA  7.11  8.84
 [5,]    NA 11 11    NA 11  8    NA  8.33 9.26    NA  7.81  8.47
 [6,]    NA 14 14    NA 14  8    NA  9.96 8.10    NA  8.84  7.04
 [7,]    NA  6  6    NA  6  8    NA  7.24 6.13    NA  6.08  5.25
 [8,]    NA  4  4    NA  4 19    NA  4.26 3.10    NA  5.39 12.50
 [9,]    NA 12 12    NA 12  8    NA 10.84 9.13    NA  8.15  5.56
[10,]    NA  7  7    NA  7  8    NA  4.82 7.26    NA  6.42  7.91
[11,]    NA  5  5    NA  5  8    NA  5.68 4.74    NA  5.73  6.89

gen.matrix

This works but it might be more convenient if a gen.matrix existed.

 as.matrix(gen.data.frame(gen.vector(i+j, i = 1:4), j = 1:4))
##      V1 V2 V3 V4
## [1,]  2  3  4  5
## [2,]  3  4  5  6
## [3,]  4  5  6  7
## [4,]  5  6  7  8

name conflicts

The code below gives an error. It is because the first argument, str=, is matched by the s= . It is easy enough to fix if you know the problem by naming the str= argument but it can seem quite mysterious when it happens. When I encountered this I was initially baffled. I didn't even realize that the name of the first argument was str= since one tends to use this function without the names. To make it worse, s= is commonly used as a subscript such as s and t being times.

I suggest that the names be changed to something that is less likely to cause a conflict such as .str= with a dot in front or some other method of dealing with this.

gen.named.vector("{s}", s*s, s = 1:2)
## Error: no named variables are given, expected at least one named variable in the '...' parameters

order of data frame rows

I would have thought that these two would result in different order of rows but they are the same. Is this intended?

> gen.data.frame(data.frame(a = i, b = j), j = 0:2, i = 1:2)
  a b
1 1 0
2 2 0
3 1 1
4 2 1
5 1 2
6 2 2
> gen.data.frame(data.frame(a = i, b = j), i = 1:2, j = 0:2)
  a b
1 1 0
2 2 0
3 1 1
4 2 1
5 1 2
6 2 2

gen.vector.char error

This gives an error:

> # ok
> library(listcompr)
> gen.vector((i + j), i = 1:3, j = 1:3)
[1] 2 3 4 3 4 5 4 5 6

> # error
> gen.vector.char(as.character(i + j), i = 1:3, j = 1:3)
Error in gen_list_internal(str, l, TRUE, OUTPUT_FORMAT$CHAR, NULL, parent.frame()) : 
  object 'i' not found

Also this gives an error. Can one only iterate over numerics?

> gen.list(paste(v, v), v = month.abb)
Error: could not evaluate variable range of 'v', got 'month.abb', expected something like start_expr:end_expr, did not find numeric vector

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.