Skip to content

Commit

Permalink
Further updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Apr 9, 2024
1 parent 6641197 commit 27a9b41
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 343 deletions.
3 changes: 3 additions & 0 deletions R/scalars.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ val_rm <- function(x, value){
sset(x, cpp_which_val(x, value, invert = TRUE))
}
}
which_val <- function(x, value, invert = FALSE){
.Call(`_cheapr_cpp_which_val`, x, value, invert)
}
4 changes: 4 additions & 0 deletions R/sset.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ sset <- function(x, ...){
#' @export
sset.default <- function(x, i, ...){
if (!missing(i) && is.logical(i)){
check_length(i, length(x))
i <- which_(i)
}
# The below line will handle a special but common
Expand Down Expand Up @@ -105,6 +106,7 @@ sset.Date <- function(x, i, ...){
# out <- sset.default(unclass(x), i, ...)
# set_attr(out, "class", oldClass(x))
if (!missing(i) && is.logical(i)){
check_length(i, length(x))
i <- which_(i)
}
if (!missing(i) &&
Expand All @@ -127,6 +129,7 @@ sset.Date <- function(x, i, ...){
#' @export
sset.POSIXct <- function(x, i, ...){
if (!missing(i) && is.logical(i)){
check_length(i, length(x))
i <- which_(i)
}
if (!missing(i) &&
Expand All @@ -149,6 +152,7 @@ sset.POSIXct <- function(x, i, ...){
#' @export
sset.factor <- function(x, i, ...){
if (!missing(i) && is.logical(i)){
check_length(i, length(x))
i <- which_(i)
}
if (!missing(i) &&
Expand Down
22 changes: 22 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ mark(sset(df, -10^4:0),
check = FALSE) # The only difference is the row names
```

The biggest difference between `sset` and `[` is the way logical vectors are handled.
The two main differences when `i` is a logical vector are:

* `NA` values are ignored, only the locations of `TRUE` values are used.
* `i` must be the same length as `x` and is not recycled.


```{r,error=TRUE}
# Examples with NAs
x <- c(1, 5, NA, NA, -5)
x[x > 0]
sset(x, x > 0)
# Example with length(i) < length(x)
sset(x, TRUE)
# This is equivalent
x[TRUE]
# to..
sset(x)
```


## Greatest common divisor and smallest common multiple

Expand Down
Loading

0 comments on commit 27a9b41

Please sign in to comment.