Skip to contents

Fix Zeros from Idle Sleep Mode

Usage

fix_zeros(df, fill_in = TRUE, by_second = FALSE, trim = FALSE)

idle_na_locf(df, by_second = FALSE)

Arguments

df

An object with columns `X`, `Y`, and `Z` or an object of class `AccData`

fill_in

Should the zeros be filled in with the last observation carried forward?

by_second

Should the last observation carried forward be done only within the same second?

trim

Should the time course be trimmed for zero values at the beginning and the end of the time course?

Value

A data set with the zeros filled in

Examples

df = data.frame(
  X = c(0.3/sqrt(0.5), rep(0, 3)),
  Y = c(0.4/sqrt(0.5), rep(0, 3)),
  Z = c(0.5/sqrt(0.5), rep(0, 3)),
  stringsAsFactors = FALSE)
fix_zeros(df)
#>           X         Y         Z
#> 1 0.4242641 0.5656854 0.7071068
#> 2 0.4242641 0.5656854 0.7071068
#> 3 0.4242641 0.5656854 0.7071068
#> 4 0.4242641 0.5656854 0.7071068
fix_zeros(df, fill_in = FALSE)
#>           X         Y         Z
#> 1 0.4242641 0.5656854 0.7071068
#> 2        NA        NA        NA
#> 3        NA        NA        NA
#> 4        NA        NA        NA
fix_zeros(df, trim = TRUE)
#>           X         Y         Z
#> 1 0.4242641 0.5656854 0.7071068
#> 2 0.4242641 0.5656854 0.7071068
#> 3 0.4242641 0.5656854 0.7071068
#> 4 0.4242641 0.5656854 0.7071068
df$time = c(1,3,2, 4)
fix_zeros(df)
#> Warning: Time is unsorted, will resort the data set
#>           X         Y         Z time
#> 1 0.4242641 0.5656854 0.7071068    1
#> 3 0.4242641 0.5656854 0.7071068    2
#> 2 0.4242641 0.5656854 0.7071068    3
#> 4 0.4242641 0.5656854 0.7071068    4
acc = list(header = NULL,
data = df
)
class(acc) = "AccData"
idle_na_locf(acc)
#> Warning: Time is unsorted, will resort the data set
#> $header
#> NULL
#> 
#> $data
#>           X         Y         Z time
#> 1 0.4242641 0.5656854 0.7071068    1
#> 3 0.0000000 0.0000000 0.0000000    2
#> 2 0.0000000 0.0000000 0.0000000    3
#> 4 0.0000000 0.0000000 0.0000000    4
#> 
#> attr(,"class")
#> [1] "AccData"
fix_zeros(acc, trim = TRUE)
#> Warning: Time is unsorted, will resort the data set
#> $header
#> NULL
#> 
#> $data
#>           X         Y         Z time
#> 1 0.4242641 0.5656854 0.7071068    1
#> 3 0.4242641 0.5656854 0.7071068    2
#> 2 0.4242641 0.5656854 0.7071068    3
#> 4 0.4242641 0.5656854 0.7071068    4
#> 
#> attr(,"class")
#> [1] "AccData"