我有两个数据帧alpha
和beta
。
dput(alpha)
structure(list(ID = c(29503L, 29507L, 29508L, 29510L),
Q_ID = structure(1:4, .Label = c("q:1392763916495:441", "q:1392763916495:445", "q:1392763916495:449", "q:1392763920794:458"),
class = "factor"),
L_Atmpt = c(0L, 0L, 0L, 0L),
Q_Atmpt = c(0L, 1L, 0L, 1L),
Q_Result = c(1L, 1L, 1L, 0L),
Time_on_Screen = c(13839L, 185162L, 264418L, 2183464L),
Start_Time = structure(1:4, .Label = c("2017-10-31Ê11:51:20", "2017-10-31Ê11:54:26", "2017-10-31Ê11:59:09", "2017-10-31Ê12:35:34"),
class = "factor"),
End_Time = structure(1:4, .Label = c("2017-10-31Ê11:51:33", "2017-10-31Ê11:57:31", "2017-10-1Ê12:03:33", "2017-10-31Ê13:11:57"),
class = "factor"),
Duration = c(173L, 55L, 98L, 1921L)),
class = "data.frame", row.names = c(NA, -4L))
dput(beta)
structure(list(ID = c(29503L, 29507L, 29508L, 29510L, 29515L, 30160L),
Q_ID = structure(1:6, .Label = c("q:1392763916495:441", "q:1392763916495:445", "q:1392763916495:449", "q:1392763920794:458", "q:1392763920794:462", "q:1392763925803:530"),
class = "factor"),
L_Atmpt = c(0L, 0L, 0L, 0L, 0L, 1L),
Q_Atmpt = c(0L, 1L, 0L, 1L, 0L, 0L),
Q_Result = c(1L, 1L, 1L, 0L, 0L, 0L),
Time_on_Screen = c(13839L, 185162L, 264418L, 2183464L, 768470L, 885800L),
Start_Time = structure(c(2L, 3L, 4L, 5L, 6L, 1L), .Label = c("2017-10-25Ê00:19:08", "2017-10-31Ê11:51:20", "2017-10-31Ê11:54:26", "2017-10-31Ê11:59:09", "2017-10-31Ê12:35:34", "2017-10-31Ê13:16:09"),
class = "factor"),
End_Time = structure(c(2L, 3L, 4L, 5L, 6L, 1L), .Label = c("2017-10-25Ê00:33:53", "2017-10-31Ê11:51:33", "2017-10-31Ê11:57:31", "2017-10-31Ê12:03:33", "2017-10-31Ê13:11:57", "2017-10-31Ê13:28:57"),
class = "factor")),
class = "data.frame", row.names = c(NA,-6L))
我想合并它们并获得最终数据帧gamma
。数据帧alpha
有一个特殊的列:alpha$duration
,我需要在数据帧beta
的末尾添加或附加。
beta
的实例多于alpha
,我想执行左连接,因此保留beta
的所有实例。这意味着列gamma$duration
的某些条目将是NULL
或NA
。
我希望NULL
或NA
将是alpha
的ID与beta
的ID不匹配的条目。但是,对于我的原始数据(具有超过10K的行和大约20个左右的变量),我得到如下信息:
ID Q_ID L_Atmpt Q_Atmpt Q_Result Time_on_Screen Start_Time End_Time Duration
29503 q:1392763916495:441 0 0 1 13839 2017-10-31Ê11:51:20 2017-10-31Ê11:51:33 NA
29507 q:1392763916495:445 0 1 1 185162 2017-10-31Ê11:54:26 2017-10-31Ê11:57:31 NA
29508 q:1392763916495:449 0 0 1 264418 2017-10-31Ê11:59:09 2017-10-31Ê12:03:33 NA
29510 q:1392763920794:458 0 1 0 2183464 2017-10-31Ê12:35:34 2017-10-31Ê13:11:57 NA
29515 q:1392763920794:462 0 0 0 768470 2017-10-31Ê13:16:09 2017-10-31Ê13:28:57 NA
30160 q:1392763925803:530 1 0 0 885800 2017-10-25Ê00:19:08 2017-10-25Ê00:33:53 NA
[不幸的是,我分享的玩具示例并未复制/捕捉我的问题。我知道想像我为什么要在原始问题中获得NA
可能具有挑战性。但是,对此的任何想法或建议将不胜感激。
作为参考,我共享了我使用的不同脚本,它们都呈现了相同的输出:
library(plyr)
gamma = join(beta, alpha, type = "left")
library(dplyr)
gamma = left_join(beta, alpha)
library(sqldf)
gamma = sqldf('SELECT beta.*, alpha.duration
FROM beta LEFT JOIN alpha
on beta.ID == alpha.ID AND
beta.Q_ID == alpha.Q_ID AND
beta.L_Atmpt == alpha.L_Atmpt AND
beta.Q_Atmpt == alpha.Q_Atmpt AND
beta.Start_Time == alpha.Start_Time')
我想提到我的原始数据框中的列alpha$duration
是在一些预处理步骤之后创建的,例如:
#Step 1: Ordering the data by ID and Start_Time
beta = beta[with(beta, order(ID, Q_ID, Q_Atmpt, Start_Time)), ]
#Step 2: End_Time lagging
library(Hmisc)
# to calculate the time difference we lag the End_Time
beta$End_Time_forward = Lag(beta$End_Time, +1)
# for comparisons, we also lag the IDs
beta$ID_forward = Lag(beta$ID, +1)
#Step 3: Now calculate the required time differences
library(sqldf)
alpha = sqldf('SELECT beta.*,
(Start_Time - End_Time_forward),
(End_Time - End_Time_forward)
FROM beta
WHERE ID_forward == ID')
#Step 4: Columns renaming
names(alpha)[names(alpha) == "(Start_Time - End_Time_forward)"] = "duration"
names(alpha)[names(alpha) == "(End_Time - End_Time_forward)"] = "end_duration"
#Step 5:Few instances have negative duration, so replace the gap between
# (last end time and current start time) with the (last end time and current
# end time) difference
alpha = alpha %>%
mutate(duration = if_else(duration < 0, end_duration, duration))
#Step 6: Convert the remaining negatives with NAs
alpha$duration[alpha$duration < 0] <- NA
#Step 7: Now replace those NAs by using the imputeTS function
library(imputeTS)
alpha$duration = na_locf(alpha$duration, option = 'locf',
na_remaining = 'rev', maxgap = Inf)
我怀疑,我操纵gamma$duration
变量的最后两个步骤可能与这种意外结果有关
我无法确定此问题的实际原因,但是,我已找到解决该问题的方法:
beta$duration = as.integer(0)
test2 = merge(x = beta, y = alpha,
by = c("ID", "Q_ID", "L_Atmpt", "Q_Atmpt", "Q_Result", "Time_on_Screen", "Start_Time", "End_Time"),
all.x = TRUE)
通过此操作,我可以访问/保留数据帧duration
的alpha
列,然后按需要使用它。