简体   繁体   中英

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.

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

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 :

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:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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