简体   繁体   English

重新编码时间序列数据以为每个人创建时间0

[英]Recoding time series data to create time 0 for each individual

I'm trying to recode some data collected every 2h such that I find the start point for each ID (ie when obs does not equal zero, ie there is data for that time point), call it time 0 and then for each subsequent time point is called 2, 4, 6 etc. 我正在尝试重新编码每2小时收集的一些数据,这样我就能找到每个ID的起始点(即,当obs不等于零时,即该时间点有数据),将其称为时间0,然后是每个后续时间点被称为2,4,6等

For eg 例如

    ID <- c("f1", "f1", "f1", "f1", "f2", "f2", "f2", "f2", "f3", "f3", "f3", "f3")
    time <- rep(c(66, 68, 70, 72), 3)
    obs <- c(1, 3, 5, 6, 0, 0, 3, 4, 0, 1, 3, 3)
    new.time <- c(0, 2, 4, 6, NA, NA, 0, 2, NA, 0, 2, 4)
    data <- as.data.frame(cbind(ID, time, obs, new.time))

Hopefully that data frame works 希望数据框架有效

i have ID, time and obs but I want to create 'new time' -- any help appreciated, particularly a dplyr solution 我有身份证,时间和视力,但我想创造'新时间' - 任何帮助,尤其是dplyr解决方案

1) We define data as a data.frame rather than a matrix in the Note at the end and then use ave to set the new.time : 1)我们将data定义为data.frame而不是最后的Note中的矩阵,然后使用ave来设置new.time

No packagtes are used. 没有使用包装。

make_no <- function(obs) c(rep(NA, sum(obs == 0)), seq(0, length = sum(obs != 0), by = 2))
transform(data, new.time = ave(obs, ID, FUN = make_no))

giving: 赠送:

   ID time obs new.time
1  f1   66   1        0
2  f1   68   3        2
3  f1   70   5        4
4  f1   72   6        6
5  f2   66   0       NA
6  f2   68   0       NA
7  f2   70   3        0
8  f2   72   4        2
9  f3   66   0       NA
10 f3   68   1        0
11 f3   70   3        2
12 f3   72   3        4

2) or using dplyr: 2)或使用dplyr:

data %>%
  group_by(ID) %>%
  mutate(new.time = make_no(obs)) %>%
  ungroup

Note 注意

ID <- c("f1", "f1", "f1", "f1", "f2", "f2", "f2", "f2", "f3", "f3", "f3", "f3")
time <- rep(c(66, 68, 70, 72), 3)
obs <- c(1, 3, 5, 6, 0, 0, 3, 4, 0, 1, 3, 3)
data <- data.frame(ID, time, obs)

We can create a custom function and apply it per group, ie 我们可以创建一个自定义函数并按组应用它,即

f1 <- function(x) {
    x1 <- length(x[x != 0])
    i1 <- seq(0, length.out = x1, by = 2)
    i2 <- c(rep(NA, (length(x) - x1)),i1)
    return(i2)
}

#Using `dplyr` to apply it,
library(dplyr)

df %>% 
 group_by(ID) %>% 
 mutate(new = f1(obs))

which gives, 这使,

 # A tibble: 12 x 4 # Groups: ID [3] ID time obs new <fct> <fct> <fct> <dbl> 1 f1 66 1 0 2 f1 68 3 2 3 f1 70 5 4 4 f1 72 6 6 5 f2 66 0 NA 6 f2 68 0 NA 7 f2 70 3 0 8 f2 72 4 2 9 f3 66 0 NA 10 f3 68 1 0 11 f3 70 3 2 12 f3 72 3 4 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM