我有两个像这些样本数据一样的数据帧(df1和df2)。我需要加入两个数据集以获取两个数据帧df1和df2,并结合它们的描述符变量(运动),以便每当状态发生变化时,都会在df1上创建新记录:
这里的目的是向每个观看间隔添加运动状态变量,我希望得到以下结果:
date id viewing_start viewing_end motion
2019-01-01 18404885 155900 155959 ON
2019-01-01 18404885 160400 160859 ON
2019-01-01 18404885 170100 170259 ON
2019-01-01 18404885 170400 170459 ON
2019-01-01 18404885 170500 171259 ON
2019-01-08 18404885 201100 201859 idle
我的数据集(viweing_start,viewing_end,start_time和end_time最初是H:M:S格式(时间)
df1 <- data.frame(
date = c("2019-01-01", "2019-01-01", "2019-01-01", "2019-01-01",
"2019-01-01", "2019-01-08", "2019-01-08", "2019-01-08",
"2019-01-08", "2019-01-08", "2019-01-08", "2019-01-08",
"2019-01-08", "2019-01-08", "2019-01-08", "2019-01-08",
"2019-01-08", "2019-01-08", "2019-01-08"),
id = c(18404885, 18404885, 18404885, 18404885, 18404885,
18404885, 18404885, 18404885, 18404885, 18404885,
18404885, 18404885, 18404885, 18404885, 18404885, 18404885,
18404885, 18404885, 18404885),
viewing_start = c(155900, 160400, 170100, 170400, 170500, 201100, 202000,
203300, 203700, 204100, 204900, 205200, 205600, 210000,
210200, 210800, 211700, 212400, 212900),
viewing_end = c(155959, 160859, 170259, 170459, 171259, 201859, 202159,
203459, 203859, 204759, 204959, 205259, 205659, 210059,
210659, 211659, 211859, 212759, 220259)
)
df2 <- data.frame(stringsAsFactors=FALSE,
id = c(18404885, 18404885, 18404885, 18404885, 18404885, 18404885,
18404885, 18404885, 18404885, 18404885, 18404885),
date = c("2019-01-01", "2019-01-01", "2019-01-01", "2019-01-01",
"2019-01-01", "2019-01-08", "2019-01-08", "2019-01-08",
"2019-01-08", "2019-01-08", "2019-01-08"),
start_time = c("3:00:00", "3:37:15", "8:06:15", "22:52:15", "25:00:00",
"3:00:00", "3:37:00", "8:29:15", "20:41:00", "20:51:00",
"22:03:15"),
end_time = c("3:37:15", "8:06:15", "22:52:15", "25:00:00", "3:00:00",
"3:37:00", "8:29:15", "20:41:00", "20:51:00", "22:03:15",
"24:12:45"),
motion = c("idle", "idle", "ON", "idle", "idle", "idle", "idle", "ON",
"WARN", "OFF", "ON")
)