Person-period 数据只是开始和结束时间相同的拼写数据的一个特例。您可以通过复制Timestamp
变量并重命名第一个start.time
和第二个来获得拼写数据stop.time
。
可能,您可以使用list=c(Machine_id, event, TDV1, TDV2, TDV3, TDV4)
asby
参数来聚合您的记录。继续进行两次,一次是FUN="min"
,一次是FUN="max"
,您应该能够找到event
和 协变量值不变的法术的开始和结束时间。
I illustrate here with an example
## creating example data
p.df <- data.frame(scan(what=list(Id=0, timestamp=0, event="", work="", income=0)))
1 2000 S working 100
1 2001 S working 100
1 2002 M working 100
1 2003 M working 100
1 2004 M jobless 80
1 2005 M jobless 70
2 2000 S jobless 10
2 2001 S working 100
2 2002 S working 100
## leave previous line blank to end scan
p.df$start <- p.df$timestamp
p.df$end <- p.df$timestamp
p.df <- p.df[,-2] ## deleting timestamp variable
bylist <- list(id = p.df$Id, event=p.df$event,
work=p.df$work, income=p.df$income)
spell1 <- aggregate(p.df[,c("start","end")], by=bylist, FUN="min")
spell2 <- aggregate(p.df[,c("start","end")], by=bylist, FUN="max")
## reordering columns
spell <- spell1[,c(1,5,6,2,3,4)]
spell[,3] <- spell2[,6] ## taking end value from spell2
spell <- spell[order(spell$id,spell$start),] ## sorting rows
spell
## id start end event work income
## 5 1 2000 2001 S working 100
## 4 1 2002 2003 M working 100
## 3 1 2004 2004 M jobless 80
## 2 1 2005 2005 M jobless 70
## 1 2 2000 2000 S jobless 10
## 6 2 2001 2002 S working 100
希望这可以帮助。