Comments (3)
posting the ever annoying "+1" here. Would it be great to have for things like modified dplyr::coalesce
for factors.
from funs.
Was thinking about vec_coalesce()
today so I figured I'd add this in
If we don't like using the common type and common size, then I think the signature should be vec_coalesce(x, ...)
to indicate that we are using the size/type of x
.
This is one place it would be nice for vec_slice <-
to know how to not recycle length 1 inputs and instead use them repeatedly
library(rlang)
library(vctrs)
vec_coalesce <- function(..., .ptype = NULL) {
args <- list2(...)
n_args <- vec_size(args)
if (n_args == 0L) {
return(NULL)
}
if (n_args == 1L) {
out <- args[[1L]]
return(out)
}
args <- vec_cast_common(!!! args, .to = .ptype)
args <- vec_recycle_common(!!! args)
out <- args[[1L]]
args <- args[-1L]
for (arg in args) {
is_na <- vec_equal_na(out)
if (!any(is_na)) {
break
}
vec_slice(out, is_na) <- vec_slice(arg, is_na)
}
out
}
vec_coalesce()
#> NULL
vec_coalesce(1, 0)
#> [1] 1
vec_coalesce(1, FALSE, .ptype = logical())
#> [1] TRUE
vec_coalesce(NA, 1)
#> [1] 1
vec_coalesce(c(1, NA, 2), 0L)
#> [1] 1 0 2
vec_coalesce(
data.frame(x = c(1, NA, 3)),
data.frame(x = 2)
)
#> x
#> 1 1
#> 2 2
#> 3 3
# a bit odd, but this technically makes sense
vec_coalesce(
data.frame(x = c(1, NA)),
data.frame(x = 2, y = 3)
)
#> x y
#> 1 1 NA
#> 2 2 3
vec_coalesce(
factor(c("x", "y", NA, "x", NA)),
factor("MISSING!")
)
#> [1] x y MISSING! x MISSING!
#> Levels: x y MISSING!
# Common size is used as the reference
vec_coalesce(
1,
c(1, 2, 3)
)
#> [1] 1 1 1
vec_coalesce(
NA,
c(1, 2, 3)
)
#> [1] 1 2 3
from funs.
I think this is just #15? No one has supplied any examples so it's hard to know.
from funs.
Related Issues (20)
- Should this repo live in tidyverse? HOT 3
- Implement is_distinct()? HOT 2
- A na.rm option for cumany? HOT 1
- na.rm argument in lead and lag? HOT 1
- Common suffixes and prefixes of vectors HOT 2
- Make sure if_else() cross-references to case_when()
- Add `mode` to vector functions
- Consider `case_when()` variant that uses values HOT 4
- `coalesce()` invariants HOT 1
- `set_intersect()`, `set_union()`, `set_diff()`, and `set_equal()`
- coalesce on a single column (typically with summarize) HOT 3
- Make a website
- Move `master` branch to `main` HOT 1
- Consider `case_when()` variant returning ordered factors HOT 3
- case_when() should preserve names HOT 1
- `between()` should recycle `left` and `right` to the size of `x`
- Should near have a digits argument?
- Accessible helper for run length encoded style groups HOT 1
- vec_equal_na is deprecated
- `min()` and `max()` variants with an `empty` argument
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from funs.