你好,我在尝试从当前数据框中创建一个数据框,但遇到了困难。事件列有两种类型,日期也有两种类型。 以下是我的数据。
id <- c(1,1,2,2,3,3)
type1 <- c("EB","EB","EB","IK","IK","EB")
date1 <- c("2011/08/31", "2011/08/31", "2012/01/15", "2012/01/20", "2012/03/10", "2012/03/24")
type2 <- c("missed", "missed", "kept", "missed", "kept", "missed")
date2 <- c("2011/03/17", "2011/03/18", "2011/03/30", "2012/04/25", "2012/05/01", "2012/05/10")
data1 <- data.frame(id, type1, date1, type2, date2)
id type1 date1 type2 date2
1 EB 2011/08/31 missed 2011/03/17
1 EB 2011/08/31 missed 2011/03/18
2 EB 2012/01/15 kept 2011/03/30
2 IK 2012/01/20 missed 2012/04/25
3 IK 2012/03/10 kept 2012/05/01
3 EB 2012/03/24 missed 2012/05/10
首先,我想将这两个日期列合并为每个id一个有序日期列。其次,我需要一个名为“event.type”的列,其中存储缺失/保留/EB/IK类别。第三,我需要一个名为“event.number”的列,其中为每个id赋予一个事件顺序号。最后,我需要一个名为“missed/kept counter”的列,用于计算每个id的缺失/保留数量。
数据应如下所示:
id <- c(1,1,1,2,2,2,2,3,3,3,3)
date <- c("2011/03/17", "2011/03/18", "2011/08/31", "2011/03/30", "2012/01/15", "2012/01/20","2012/04/25","2012/03/10","2012/03/24","2012/05/01","2012/05/10")
event.type <- c("missed", "missed", "EB", "kept", "EB", "IK", "missed", "IK", "EB", "kept", "missed")
event.number <- c(1,2,3,1,2,3,4,1,2,3,4)
missed.kept.counter <- c(1,2,0,1,0,0,1,0,0,1,2)
data2 <- data.frame(id,date,event.type,event.number,missed.kept.counter)
> data2
id date event.type event.number missed.kept.counter
1 2011/03/17 missed 1 1
1 2011/03/18 missed 2 2
1 2011/08/31 EB 3 0
2 2011/03/30 kept 1 1
2 2012/01/15 EB 2 0
2 2012/01/20 IK 3 0
2 2012/04/25 missed 4 1
3 2012/03/10 IK 1 0
3 2012/03/24 EB 2 0
3 2012/05/01 kept 3 1
3 2012/05/10 missed 4 2
我期待任何帮助让我摆脱这个问题。
提前感谢你的帮助。
祝一切顺利。
type1
是否对应于date1
,type2
是否对应于date2
?这意味着它们可以是两个独立的数据框,然后根据id
进行合并吗? - Gaurav Bansal