我有一個包含 100000 行和多個變數/列的資料框,我想從中
- 根據“Y”列中的值計算特定操作的持續時間。Y 列有多個值 0 和 1 序列,每當發生動作時,值 1序列中的 1(就在下一個 0 之前)。對于所有 1 和 0 的每一個對應行,當前運行時的“X”列中總是有一個時間戳,因此時間差基本上可以通過簡單的減法計算得出:
TIME_OF_FINAL_1_IN_SEQUENCE minus TIME_OF_FIRST_1_IN_SEQUENCE
對于所有不同的序列,相同的計算將重復多次,并且將創建一個列出操作的所有不同持續時間的新資料框。
- 以類似的方式,對于“Z”列中的值,計算從一個序列的前 1 個到所有不同序列的一個序列的最后一個的周期的平均值、標準偏差、最小值和最大值. 然后將所有資料組合為一個資料幀并將其匯出為 csv 檔案,其中應包含“動作持續時間”、“Z avg”、“Z std”、“Z min”、“Z max”和“ id”列來自原始資料框。我怎么能在 R 中撰寫這樣的腳本?
偽樣式代碼可能看起來像這樣:
for all the rows in df {
if (number 1 in column Y) {
from first 1 until the last 1 in a sequence: calculate TIME_OF_FINAL_1_IN_SEQUENCE minus TIME_OF_FIRST_1_IN_SEQUENCE from column X
ALSO from the range of first value of 1 to the last value of 1 in this sequence of 1: calculate avg, std, min, and max for the variable Z
if (number 0) in column
add new element/row to the list (including the variables of: "action duration", "Z avg", "Z std", "Z min", "Z max" and the "id") and move to the next 1
(不確定偽代碼中的演算法是否正是我在文本中描述的,但至少我盡我所能在此處包含某種“代碼示例”:-))
uj5u.com熱心網友回復:
我相信您有多個可能的連續行的 1 和 0 序列。我認為該方法是為每個序列生成一個唯一識別符號,并估計每個識別符號所需的統計資訊。這很容易使用data.table, 和data.table::rleied
library(data.table)
setDT(dt)
dt[,seqid:= rleid(Y)] %>%
.[Y==1,.(
seq_dur = as.numeric(max(time)-min(time)),
meanZ = mean(Z),
stdZ=sd(Z),
minZ = min(Z),
maxZ=max(Z)), by=.(seqid)]
輸出:
seqid seq_dur meanZ stdZ minZ maxZ
1: 1 4 -0.41937718 0.7936389 -1.15956013 0.7945978
2: 3 5 -0.17031761 0.8429274 -1.41463319 0.8502819
3: 5 29 -0.01909116 1.1878013 -2.32238540 2.7392739
4: 7 17 -0.14040415 0.9600719 -1.82184504 1.1401493
5: 9 6 0.14154931 1.1930633 -1.55719089 1.6827525
---
2431: 4861 13 -0.17095911 0.9193558 -2.14869215 1.1571597
2432: 4863 27 -0.06239130 1.0546947 -2.46668844 2.2189060
2433: 4865 27 -0.22289381 1.0330064 -2.32818061 2.6114507
2434: 4867 2 0.42001740 0.8060201 -0.09206373 1.3491093
2435: 4869 3 -0.68025767 1.5678846 -2.96307855 0.5092229
輸入:
set.seed(123)
dt = data.table(
Y = unlist(lapply(1:10000, \(x) rep(sample(c(1,0),1), times=sample(3:8,1))))
)
dt[, time := seq(as.POSIXct("2022/1/1"),by=1, length.out=nrow(dt))]
dt[, Z:=rnorm(nrow(dt))]
head(dt)
Y time Z
1: 1 2022-01-01 00:00:00 -0.152767922
2: 1 2022-01-01 00:00:01 -1.159560131
3: 1 2022-01-01 00:00:02 0.794597776
4: 1 2022-01-01 00:00:03 -1.065863531
5: 1 2022-01-01 00:00:04 -0.513292070
6: 0 2022-01-01 00:00:05 -1.065909875
7: 0 2022-01-01 00:00:06 -0.643175787
8: 0 2022-01-01 00:00:07 0.817414048
9: 0 2022-01-01 00:00:08 -0.629111341
10: 0 2022-01-01 00:00:09 1.491066477
11: 0 2022-01-01 00:00:10 0.233849804
12: 0 2022-01-01 00:00:11 -0.007799405
13: 0 2022-01-01 00:00:12 -1.314916805
14: 0 2022-01-01 00:00:13 0.335385778
15: 0 2022-01-01 00:00:14 -0.093167347
16: 0 2022-01-01 00:00:15 0.646596214
17: 0 2022-01-01 00:00:16 -0.969331732
18: 0 2022-01-01 00:00:17 1.681191187
19: 0 2022-01-01 00:00:18 0.357307413
20: 1 2022-01-01 00:00:19 -0.940199141
21: 1 2022-01-01 00:00:20 0.059556026
22: 1 2022-01-01 00:00:21 0.098646529
23: 1 2022-01-01 00:00:22 0.324442236
24: 1 2022-01-01 00:00:23 -1.414633187
25: 1 2022-01-01 00:00:24 0.850281851
Y time Z
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/445059.html
