如何在行程编码(RLE)转换后将数据转换为常规表格数据集

e5nqia27  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(85)

我有使用Run Length Encoding变换形成的观测
作为示例

set.seed(1)
make_data <- function() {
  series <- rnorm(sample(10:50,1)) |> cumsum() |> sign()
  accelerometry::rle2(series,indices = T)
}

my_obs <- lapply(1:5,\(x) make_data())
names(my_obs) <- paste("observation", 1:length(my_obs))

字符串
..

my_obs

$`observation 1`
     value start stop length
[1,]    -1     1    3      3
[2,]     1     4    5      2
[3,]    -1     6    6      1
[4,]     1     7   30     24

$`observation 2`
     value start stop length
[1,]     1     1    5      5
[2,]    -1     6    8      3
[3,]     1     9   30     22

$`observation 3`
     value start stop length
[1,]     1     1   30     30

$`observation 4`
     value start stop length
[1,]    -1     1    1      1
[2,]     1     2   12     11
[3,]    -1    13   15      3
[4,]     1    16   30     15

$`observation 5`
     value start stop length
[1,]    -1     1    1      1
[2,]     1     2    9      8
[3,]    -1    10   30     21


如何将这些数据转换为常规的表视图(数据集),其中每个observation都是一行。如何做到正确而不丢失信息?

的数据
谁不知道Run Length Encoding是什么,请阅读

?rle
?accelerometry::rle2

xqk2d5yq

xqk2d5yq1#

订正的

library(tidyverse)

(step_1 <- imap_dfr(my_obs, data.frame, .id = "id") |> select(all_of(c(
  "id", "value", "start", "stop", "length"
))))

(step_2 <- step_1 |> pivot_longer(cols = -id) |> group_by(id) |>
  mutate(counter_id = cumsum(name == "value")))

(fin <- step_2 |>
  pivot_wider(
    id_cols = c("id"),
    names_from = c("name", "counter_id")
  ))

字符串

相关问题