R - 使用逻辑条件并使用 >、< 和 = 运算符连接 R 中的表
R - join tables in R using logical conditions and using >, < and = operator
在 R 中有点新,但正在尝试弄明白。
所以,我有一个来自 MySQL 的查询,它根据他们的调谐时间(登录)连接两个表并结合电视时间表,我可以检查他们在看什么。
在SQL我可以这样做:
SELECT
s.Device,
s.CallLetter AS Channel,
e.Title AS Program_Title,
e.TitleId AS Program_Title_ID,
s.TuneDuration AS Stream_Time,
s.TuneTime AS Login_Time,
e.StartDate AS StartTime,
e.EndDate AS End_Time
FROM
sessoeslive_canais s
JOIN
epg e ON s.TuneTime >= e.StartDate
AND s.TuneTime <= e.EndDate
AND s.CallLetter = e.CallLetter
但是现在,在 R 中,这个问题似乎更大了。
我写过类似的东西:
join <-
sessoeslive_canais %>%
left_join(epg, by = c("CallLetter" == "CallLetter", "TuneTime" > "StarDate", "TuneTime" < "EndDate" ))
关于如何在 R 中执行我在 SQL 中表达的内容,您有什么建议吗?
这里我们可以使用fuzzyjoin
library(fuzzyjoin)
fuzzy_left_join(sessoeslive_canais, epg, by = c("CallLetter" == "CallLetter",
"TuneTime" = "StarDate", "TuneTime" = "EndDate" ),
match_fun = list(`==`, `>`, `<`)) %>%
select(Program_Title = Title.y, Program_Title_ID = TitleId.y,
Stream_Time = TuneDuration.x, Login_Time = TuneTime.x,
StartTime = StartDate.y, End_Time = EndDate.y)
或者使用来自 data.table
的非等值连接
library(data.table)
setDT(sessoeslive_canais)[epg, on = .(CallLetter,
TuneTime > StartDate, TuneTime < EndDate)]
在 R 中有点新,但正在尝试弄明白。
所以,我有一个来自 MySQL 的查询,它根据他们的调谐时间(登录)连接两个表并结合电视时间表,我可以检查他们在看什么。
在SQL我可以这样做:
SELECT
s.Device,
s.CallLetter AS Channel,
e.Title AS Program_Title,
e.TitleId AS Program_Title_ID,
s.TuneDuration AS Stream_Time,
s.TuneTime AS Login_Time,
e.StartDate AS StartTime,
e.EndDate AS End_Time
FROM
sessoeslive_canais s
JOIN
epg e ON s.TuneTime >= e.StartDate
AND s.TuneTime <= e.EndDate
AND s.CallLetter = e.CallLetter
但是现在,在 R 中,这个问题似乎更大了。
我写过类似的东西:
join <-
sessoeslive_canais %>%
left_join(epg, by = c("CallLetter" == "CallLetter", "TuneTime" > "StarDate", "TuneTime" < "EndDate" ))
关于如何在 R 中执行我在 SQL 中表达的内容,您有什么建议吗?
这里我们可以使用fuzzyjoin
library(fuzzyjoin)
fuzzy_left_join(sessoeslive_canais, epg, by = c("CallLetter" == "CallLetter",
"TuneTime" = "StarDate", "TuneTime" = "EndDate" ),
match_fun = list(`==`, `>`, `<`)) %>%
select(Program_Title = Title.y, Program_Title_ID = TitleId.y,
Stream_Time = TuneDuration.x, Login_Time = TuneTime.x,
StartTime = StartDate.y, End_Time = EndDate.y)
或者使用来自 data.table
library(data.table)
setDT(sessoeslive_canais)[epg, on = .(CallLetter,
TuneTime > StartDate, TuneTime < EndDate)]