は
まず、入力データフレームを設定し、データを設定します。 A
とB
の2つのバージョンのデータフレームを作成します。At
とBt
は、時間のためのクラス"times"
のchronパッケージを使用しています("character"
クラスよりも加減算が可能です):
LinesA <- "OBS ID StartTime Duration Outcome
1 01 10:12:06 00:00:10 Normal
2 02 10:12:30 00:00:30 Weird
3 01 10:15:12 00:01:15 Normal
4 02 10:45:00 00:00:02 Normal"
LinesB <- "OBS ID Time
1 01 10:12:10
2 01 10:12:17
3 02 10:12:45
4 01 10:13:00"
A <- At <- read.table(textConnection(LinesA), header = TRUE,
colClasses = c("numeric", rep("character", 4)))
B <- Bt <- read.table(textConnection(LinesB), header = TRUE,
colClasses = c("numeric", rep("character", 2)))
# in At and Bt convert times columns to "times" class
library(chron)
At$StartTime <- times(At$StartTime)
At$Duration <- times(At$Duration)
Bt$Time <- times(Bt$Time)
回クラスとsqldf
今、私たちはsqldfパッケージを使用して計算を行うことができます。私たちは、(出力にクラスを割り当てることはありません)method="raw"
を使用するので、出力"Time"
コラム私たち自身に"times"
クラスを割り当てる必要があります。
library(sqldf)
out <- sqldf("select Bt.OBS, ID, Time, Outcome from At join Bt using(ID)
where Time between StartTime and StartTime + Duration",
method = "raw")
out$Time <- times(as.numeric(out$Time))
結果は次のとおりです。の開発バージョンでは
> out
OBS ID Time Outcome
1 1 01 10:12:10 Normal
2 3 02 10:12:45 Weird
sqldfこれはmethod="raw"
を使用せずに実行することができ、列は、sqldfクラス割り当てヒューリスティックによって自動的に"times"
クラスに設定されます。
library(sqldf)
source("http://sqldf.googlecode.com/svn/trunk/R/sqldf.R") # grab devel ver
sqldf("select Bt.OBS, ID, Time, Outcome from At join Bt using(ID)
where Time between StartTime and StartTime + Duration")
に文字クラスとsqldf実際に可能
そのないはsqliteののstrftime機能を採用した文字列のうち、sqliteの中のすべての時間の計算を行うことにより、"times"
クラスを使用します。 SQLステートメントは、残念ながら、もう少し複雑である。
sqldf("select B.OBS, ID, Time, Outcome from A join B using(ID)
where strftime('%s', Time) - strftime('%s', StartTime)
between 0 and strftime('%s', Duration) - strftime('%s', '00:00:00')")
EDIT:
、文法を修正し、追加のアプローチを加え、固定/ read.table
文を改善編集の一連。
EDIT:
簡体/改善最終sqldf声明。
うわー。このような徹底的な答えをありがとう。 – bnjmn