time - R - Split numeric vector into intervals -
i have question regarding "splitting" of vector, although different approaches might feasible. have data.frame(df) looks (simplified version):
case time 1 1 5 2 2 3 3 3 4 the "time" variable counts units of time (days, weeks etc) until event occurs. expand data set increasing number of rows , "split" "time" intervals of length 1, beginning @ 2. result might this:
case time begin end 1 1 5 2 3 2 1 5 3 4 3 1 5 4 5 4 2 3 2 3 5 3 4 2 3 6 3 4 3 4 obviously, data set bit larger example. feasible method achieve result?
i had 1 idea of beginning with
df.exp <- df[rep(row.names(df), df$time - 2), 1:2] in order expand number of rows per case, according number of time intervals. based on this, "begin" , "end" column might added in fashion of:
df.exp$begin <- 2:(df.exp$time-1) however, i'm not successful @ creating respective columns, because command uses first row calculate (df.exp$time-1) , doesn't automatically distinguish "case".
any ideas appreciated!
you can try
df2 <- df1[rep(1:nrow(df1), df1$time-2),] row.names(df2) <- null m1 <- do.call(rbind, map(function(x,y) { v1 <- seq(x,y) cbind(v1[-length(v1)],v1[-1l])}, 2, df1$time)) df2[c('begin', 'end')] <- m1 df2 # case time begin end #1 1 5 2 3 #2 1 5 3 4 #3 1 5 4 5 #4 2 3 2 3 #5 3 4 2 3 #6 3 4 3 4 or option data.table
library(data.table) setdt(df1)[,{tmp <- seq(2, time) list(time= time, begin= tmp[-length(tmp)], end=tmp[-1])} , = case] # case time begin end #1: 1 5 2 3 #2: 1 5 3 4 #3: 1 5 4 5 #4: 2 3 2 3 #5: 3 4 2 3 #6: 3 4 3 4
Comments
Post a Comment