合并数据集时如何去除重复观测并匹配trialnumber
Hey there, let's fix that annoying duplicate row issue you're seeing after the left join! The root problem here is how you're connecting the two datasets—right now you're only matching on sq_id, which means every row in export for a given sq_id gets paired with every row in timebtw for the same sq_id. That's why you're getting way more rows than expected.
问题原因拆解
When you use left_join(export, timebtw, by = "sq_id"), R doesn't care about matching trialnumber values—it just creates every possible combination of rows with the same sq_id. For your sq_id 10212 example, that's 4 rows in export × 4 rows in timebtw = 16 rows total, which is way more than the 4 you want.
解决方案1:修改连接键,同时匹配sq_id和trialnumber
The simplest fix is to add trialnumber to the by argument so R matches rows that have both the same sq_id and trialnumber:
# 确保两个数据集的trialnumber列名一致,然后同时按两个字段连接 new <- dplyr::left_join(export, timebtw, by = c("sq_id", "trialnumber"))
This way, each row in export only pairs with the exact matching row in timebtw for the same individual and trial number—no duplicates, just clean 1:1 matches.
解决方案2:直接在原数据集计算(更高效,避免额外Join)
Even better, you don't need to create a separate timebtw dataset at all. You can calculate time_gap directly in the original export dataframe using dplyr group operations, which saves you the join step entirely:
export <- export %>% # 先转换日期时间格式,注意要包含小时分钟的格式符 mutate( trialdate = as.POSIXct(trialdate, format = "%m/%d/%y"), datetime = as.POSIXct(paste(trialdate, trialtime), format = "%Y-%m-%d %H:%M", usetz = FALSE) ) %>% # 按个体分组计算时间间隔 group_by(sq_id) %>% mutate( # 计算当前试验与该个体最早试验的时间差,转换为天数 time_gap = as.numeric(datetime - min(datetime), units = "days") ) %>% ungroup()
效果验证
For your sq_id 10212 example, either solution will give you exactly 4 rows, each with the correct time_gap matched to its trialnumber—just like your desired output. No duplicates, no extra messy columns.
小提醒:原代码里的data应该是export吧?替换成export避免混淆,另外转换datetime时要加上%H:%M,不然会忽略trialtime的小时分钟部分哦。
内容的提问来源于stack exchange,提问作者Blundering Ecologist

