Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: LightLogR
Title: Process Data from Wearable Light Loggers and Optical Radiation Dosimeters
Version: 0.10.1
Version: 0.10.2
Authors@R: c(
person("Johannes", "Zauner",
email = "johannes.zauner@tum.de", role = c("aut", "cre"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# LightLogR 0.10.2

* `add_states()` no longer creates duplicate observations at identical start/endpoints. The default behavior is now that the `start` is inclusive and `end` is exclusive. Adjust the new `bounds` argument for more options.

# LightLogR 0.10.1

* `extract_metric()` give more helpful error messages when using a base dataset without start/end columns.
Expand Down
15 changes: 13 additions & 2 deletions R/add_states.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
#' will be forced to the same time zone as the `dataset` using
#' [lubridate::force_tz()]. If `FALSE` (default), the start and end times of
#' the `States.dataset` will be used as is.
#' @param bounds How to treat the edge case, when a state start or endpoint
#' falls onto a data point. See [dplyr::join_by()] for details on the bounds
#' argument (Join types > Overlap joins > between). Basically `[` or `]`
#' signal an inclusive joint, whereas `(` or `)` signal exclusivity.
#'
#' @returns a modified `dataset` with the states added. The states are added as
#' new columns to the `dataset`. The columns are named after the columns in
Expand Down Expand Up @@ -65,6 +69,7 @@ add_states <- function(dataset,
start.colname = start,
end.colname = end,
force.tz = FALSE,
bounds = c("[)", "[]", "(]", "()"),
Comment thread
JZauner marked this conversation as resolved.
leave.out = c("duration", "epoch")
){
# Initial Checks ----------------------------------------------------------
Expand All @@ -75,6 +80,8 @@ add_states <- function(dataset,
"States.dataset is not a dataframe" = is.data.frame(States.dataset)
)

bounds <- rlang::arg_match(bounds)

# Check if Datetime.colname is part of the dataset
Datetime.colname.defused <- colname.defused({{ Datetime.colname }})
if (!Datetime.colname.defused %in% names(dataset)) {
Expand Down Expand Up @@ -171,8 +178,12 @@ add_states <- function(dataset,
by =
dplyr::join_by(
!!!groups,
{{ Datetime.colname }} >= {{ start.colname}},
{{ Datetime.colname }} <= {{ end.colname }}
dplyr::between({{ Datetime.colname }},
{{ start.colname}},
{{ end.colname }},
bounds = !!bounds)
# {{ Datetime.colname }} >= {{ start.colname}},
# {{ Datetime.colname }} <= {{ end.colname }}
)
) |>
dplyr::select(-{{ start.colname}}, -{{ end.colname }})
Expand Down
6 changes: 6 additions & 0 deletions man/add_states.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/import_Dataset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkgdown/assets/pdf/Online_course_series_2025-26.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/testthat/test-extract_states.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ test_that("add_states basic functionality", {
Intensity = c(5, 3)
) # No group needed if main has only one group that matches

result <- add_states(main_data, states_data, Datetime.colname = Timestamp)
result <- add_states(main_data, states_data, Datetime.colname = Timestamp, bounds = "[]")

expect_s3_class(result, "tbl_df")
expect_equal(nrow(result), 4) # Same as main_data
Expand Down Expand Up @@ -226,7 +226,7 @@ test_that("add_states with multiple groups and NAs for non-matching times", {
StateInfo = c("StateA_Mid", "StateB_Start")
) %>% dplyr::group_by(Id) # Match grouping

result <- add_states(main_data, states_data)
result <- add_states(main_data, states_data, bounds = "[]")
# For Id A: Datetime 0,1,2. State defined for 1h.
# For Id B: Datetime 0,1,2. State defined for 0h.
expect_equal(result$StateInfo[result$Id == "A"], c(NA, "StateA_Mid", NA))
Expand Down
Binary file modified vignettes/articles/images/Day.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading