dfilter: Distance filter

View source: R/dfilter.R

dfilterR Documentation

Distance filter

Description

Filter entries of a vector d of class numeric, integer, Date, or POSIXt according to maximal distance max.dist.

Usage

dfilter(
  d,
  min.dist = 0,
  max.dist = Inf,
  temporal.unit = c("days", "seconds", "minutes", "hours", "weeks", "years"),
  which.chunk = "all"
)

Arguments

d

a numeric, integer, Date, or POSIXt

min.dist

numeric specifying minimal distance that entries in d are allowed to have to be retained

max.dist

numeric specifying maximal distance that entries in d are allowed to have to be retained

temporal.unit

only applies, if d is of class Date or POSIXt; accepted values are "days", "seconds", "minutes", "hours", "weeks", "years".

which.chunk

controls output and display of chunks satisfying the restriction imposed by max.dist; accepted accepted values are all", "biggest", "first", and "random"

Value

depending on which.chunk a logical ("biggest", "first", and "random") labeling entries in d to be retained or a data.frame ("all") containing retained entries and the chunk number.

Examples

# test data
set.seed(1)
test <- data.frame(group = sample(letters[1:4], 20, TRUE),
                  pos = runif(20, 0, 100),
                  time = as.POSIXct(runif(20, 0, as.numeric(Sys.time())),
                                    origin = as.POSIXct("1970-01-01 00:00.00 UTC")))

# indicate first chunk satisfying the condition set by max.dist
dfilter(test$pos, max.dist = 15, which.chunk = "first")
# show all chunks
dfilter(test$pos, max.dist = 15, which.chunk = "all")
# apply in a grouped tibble
test %>% group_by(group) %>% filter(dfilter(time, max.dist = 1000, which.chunk = "first"))
# please note that in a grouped tibble, you cannot use dfilter inside filter()
test %>% group_by(group) %>% group_modify(~ dfilter(.x[, "pos"], max.dist = 15, which.chunk = "all"))

joheli/kungfu documentation built on March 25, 2024, 10:10 a.m.