cynkra / constructive

Display Idiomatic Code to Construct Most R Objects
https://cynkra.github.io/constructive
Other
126 stars 6 forks source link

compress repeated value #470

Closed ggrothendieck closed 2 weeks ago

ggrothendieck commented 1 month ago

In this example we did not need rep. It could have been written demand = 1 .

BOD$demand <- 1
construct(BOD)
## data.frame(Time = c(1, 2, 3, 4, 5, 7), demand = rep(1, 6L)) |>
##  structure(reference = "A1.4, p. 270")
moodymudskipper commented 1 month ago

Fair point. We can recycle constant columns, If all columns are constant we can recycle only from the 2nd column however, even if row names are provided.

Since compression is an attribute of atomic vector construction and we're with data frames I would make it permanent, not dependent on compress = TRUE.

We could also be clever by recycling vectors of length > 1 but in that case I think rep() is better.